我构建了一个片段(使用 ConstraintLayout),如下所示: Fragment image 我想重用它,例如将它(添加到 fragmentManager)加载到 Activity 的“片段持有者”,但如果这个持有者太小,图像会互相覆盖。看到这个: 图像问题 所以,图像不会调整大小......有没有实现自动调整大小的解决方案?
谢谢,佐尔坦
我构建了一个片段(使用 ConstraintLayout),如下所示: Fragment image 我想重用它,例如将它(添加到 fragmentManager)加载到 Activity 的“片段持有者”,但如果这个持有者太小,图像会互相覆盖。看到这个: 图像问题 所以,图像不会调整大小......有没有实现自动调整大小的解决方案?
谢谢,佐尔坦
将您的每一个ImageViews放在自己的ConstraintLayout. 的layout_widthand应该layout_height是. 他们父母的and应该是and分别。ImageViewswrap_contentlayout_widthlayout_heightConstraintViewsmatch_parentwrap_content
使用,和将左侧锚定ConstraintView到父级的顶部和开始,将右侧锚定ConstraintView到顶部和结束。layout_constraintTop_toTopOflayout_constraintStart_toStartOflayout_constraintEnd_toEndOf
ConstraintViews还使用layout_marginTopandlayout_marginLeft为左侧添加边距;layout_marginTop和layout_marginRight权利。
接下来,创建一个vertical Guideline作为片段的子级。将其设置layout_constraintGuide_percent为0.5。给它一个id。@+id/guideline
将左侧设置ConstraintView's layout_constraintRight_toLeftOf为@+id/guideline,将右侧ConstraintView's layout_constraintLeft_toRightOf设置@+id/guideline为 。
ImageViews如果片段不是那么宽,这应该缩小尺寸。
如果您希望两个图像之间有最小间隙,您可以添加layout_marginRight到左侧ConstraintView和layout_marginLeft右侧ConstraintView。将每个设置为2dp将给出 的最小间隙4dp。
这是一个示例布局文件。编辑容器ConstraintLayout's layout_width以查看它的运行情况:
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="100dp"
android:layout_height="300dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:background="#333333">
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline"
app:layout_constraintGuide_percent="0.5"
android:orientation="vertical"/>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
app:layout_constraintRight_toLeftOf="@+id/guideline"
android:layout_marginRight="2dp"
android:background="#FF0000">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@mipmap/ic_launcher"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="0dp"/>
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
app:layout_constraintLeft_toRightOf="@+id/guideline"
android:layout_marginLeft="2dp"
android:background="#00FF00">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@mipmap/ic_launcher"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="0dp"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>