android – 具有固定宽高比的ConstraintLayout链
作者:互联网
我正在尝试根据以下布局构建一个包含3个imageView的活动:
<------W-------> <------W-------->
^ +---------------+--------+---------------+
| | | | |
| | | | |
H | A | B | C |
| | | (1:3) | |
| | | | |
v +---------------+--------+---------------+
> H应该是父高度的1/3
> B的纵横比固定为1:3
> A和C应具有相同的宽度
我尝试了多种解决方案,却找不到有效的解决方案.最后一个使用:
>指南设置为父高度的33%
> A,B和C之间的水平链
> B的固定纵横比
> A,B和C使用match_constraint维度
虽然我希望解算器能够制作出所需的布局,但似乎链条定义了与所有视图相似的宽度,如截图中所示.
我也尝试使用LinearLayout,但似乎无法修复一个项目的宽高比.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_blue_bright"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintDimensionRatio=""
app:layout_constraintEnd_toStartOf="@+id/imageView2"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_blue_bright"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintDimensionRatio="1:3"
app:layout_constraintEnd_toStartOf="@+id/imageView3"
app:layout_constraintStart_toEndOf="@+id/imageView1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_blue_bright"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@mipmap/ic_launcher" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3333" />
<android.support.constraint.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.6666666666" />
</android.support.constraint.ConstraintLayout>
非常感谢!
解决方法:
做你要求的事情可能有几种方法.这是一种方法:
此布局的关键是首先按照XML中的定义设置中心图像.一旦建立了中心图像,就可以更容易地定义左图像和右图像.
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageViewLeft"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@+id/imageCenter"
app:layout_constraintEnd_toStartOf="@+id/imageCenter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageCenter"
app:srcCompat="@color/colorPrimary" />
<ImageView
android:id="@+id/imageCenter"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="W,1:3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.33"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@color/colorAccent" />
<ImageView
android:id="@+id/imageViewRight"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@+id/imageCenter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageCenter"
app:layout_constraintTop_toTopOf="@+id/imageCenter"
app:srcCompat="@color/colorPrimary" />
</android.support.constraint.ConstraintLayout>
标签:android,aspect-ratio,android-constraintlayout,constraint-layout-chains 来源: https://codeday.me/bug/20190627/1304155.html