我建了一个片段(与ConstraintLayout),它看起来像这样: Fragment image 我想重新使用它,例如加载它(添加到fragmentManager)到活动的“片段持有人”,但如果该持有人太小,图像将互相覆盖。 看到这个: Image problem 因此,图像不会调整大小...有没有解决方案来实现自动调整大小?拉伸ConstraintLayout及其内容
感谢, 佐尔坦
我建了一个片段(与ConstraintLayout),它看起来像这样: Fragment image 我想重新使用它,例如加载它(添加到fragmentManager)到活动的“片段持有人”,但如果该持有人太小,图像将互相覆盖。 看到这个: Image problem 因此,图像不会调整大小...有没有解决方案来实现自动调整大小?拉伸ConstraintLayout及其内容
感谢, 佐尔坦
将在自己的ConstraintLayout
每个ImageViews
的。 ImageViews
的layout_width
和layout_height
应该是wrap_content
。其母公司ConstraintViews
的layout_width
和layout_height
应分别为match_parent
和wrap_content
。
锚左ConstraintView
到顶部,并开始父,右ConstraintView
使用layout_constraintTop_toTopOf
,layout_constraintStart_toStartOf
,并且layout_constraintEnd_toEndOf
顶部和结束。
同时使用layout_marginTop
和layout_marginLeft
为左侧添加页边距ConstraintViews
; layout_marginTop
和layout_marginRight
的权利。
接下来,创建一个vertical
Guideline
作为您的片段的孩子。将其layout_constraintGuide_percent
设置为0.5
。给它@+id/guideline
的id
。
设置左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>
不错的办法! 我试图简化我的真实问题,所以我说我有两个图像,但我有四个...在一行中,我有4行...所以我有16个图像。 :) – Zoltan
(突然发表我的评论...) 所以,你的解决方案看起来不错,但我不能用来解决我的真正问题... :( 更重要的是,我遇到了一个奇怪的Android Studio问题: 在文本模式我向我的图像写入'match_parent',然后点击设计,然后返回文本,Studio将它替换为固定值!:O为什么? – Zoltan