我在Constraint Layout中有一个ImageView,如下所示
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/argentina"
android:layout_marginLeft="16dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.501"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp" />
所示
请注意,Imageview会随着约束布局的大小而变化
答案 0 :(得分:1)
是的,您可以使用约束 链和 match_constraint(0dp)属性实现这一目标 你必须在这里定义垂直 和 水平 链到下面的所有按钮是相同的样本
<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:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/firefox"
android:layout_marginLeft="16dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.501"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp" />
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/imageView"
app:layout_constraintStart_toEndOf="@+id/button5"
app:layout_constraintBottom_toTopOf="@id/button41"
app:layout_constraintVertical_chainStyle="spread_inside"/>
<Button
android:id="@+id/button31"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/imageView"
app:layout_constraintBottom_toTopOf="@id/button311"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintEnd_toStartOf="@+id/button51"
app:layout_constraintVertical_chainStyle="spread_inside" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintTop_toTopOf="@+id/imageView"
app:layout_constraintEnd_toStartOf="@+id/button11"
app:layout_constraintStart_toEndOf="@+id/button13"
app:layout_constraintBottom_toTopOf="@+id/button133"
app:layout_constraintVertical_chainStyle="spread_inside" />
<Button
android:id="@+id/button311"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/button31"
app:layout_constraintBottom_toTopOf="@+id/button13"
app:layout_constraintEnd_toStartOf="@+id/button133"
app:layout_constraintHorizontal_chainStyle="spread_inside" />
<Button
android:id="@+id/button133"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintStart_toEndOf="@+id/button311"
app:layout_constraintEnd_toStartOf="@+id/button41"
app:layout_constraintTop_toBottomOf="@+id/button5"
app:layout_constraintBottom_toTopOf="@+id/button51" />
<Button
android:id="@+id/button41"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="@+id/imageView"
android:text="Button"
app:layout_constraintTop_toBottomOf="@+id/button1"
app:layout_constraintBottom_toTopOf="@+id/button11"
app:layout_constraintStart_toEndOf="@+id/button133" />
<Button
android:id="@+id/button11"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintStart_toEndOf="@+id/button51"
app:layout_constraintTop_toBottomOf="@id/button41"
/>
<Button
android:id="@+id/button13"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@id/button311"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintEnd_toStartOf="@+id/button51" />
<Button
android:id="@+id/button51"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintEnd_toStartOf="@+id/button11"
app:layout_constraintStart_toEndOf="@+id/button13"
app:layout_constraintTop_toBottomOf="@id/button133"/>
</android.support.constraint.ConstraintLayout>
您可以在此处获取有关链和约束的所有详细信息 https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#Chains
这将适用于所有设备尺寸和方向
注意:我建议您在图片视图中添加一些minHeight,以免隐藏按钮,但我不知道您的要求,但这是一个明显的观点