我在这里遇到了这个问题,如果我像这样使用嵌套的linearlayout,很容易解决这个问题:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:layout_marginTop="32dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content">
<ImageView/>
<TexView/>
</LinearLayout>
<LinearLayout android:layout_marginTop="32dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content">
<ImageView/>
<TexView/>
</LinearLayout>
<LinearLayout android:layout_marginTop="32dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content">
<ImageView/>
<TexView/>
</LinearLayout>
</LinearLayout>
但有没有办法在约束布局中解决这个问题? 在图像中,正方形表示固定大小的图像视图,矩形是文本视图,可以是1行(高于imageview的高度)或多行(高度大于imageview)
我尝试用xDp间隔每个图像视图的约束,如果所有文本视图都不高于图像视图,则可以,但如果文本视图高于图像视图,则它将重叠。 我也尝试从ImageView到TextView的间距,但是如果TextView小于ImageView,则间距将再次出错。
有没有办法在ConstraintLayout中解决这个问题?
及其布局xml
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_timer_black_24dp"
app:layout_constraintEnd_toEndOf="@+id/imageView3"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="@+id/imageView3" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_timer_black_24dp"
app:layout_constraintEnd_toEndOf="@+id/imageView"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_timer_black_24dp"
tools:layout_editor_absoluteX="40dp"
tools:layout_editor_absoluteY="32dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:fontFamily="@font/source_sans"
android:text="This text is a lot longer and overlaps the one below which is bad."
android:textColor="@color/primary_text"
android:textSize="24sp"
android:typeface="normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/imageView" />
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:fontFamily="@font/source_sans"
android:text="This is a normal length text and that makes it."
android:textColor="@color/primary_text"
android:textSize="24sp"
android:typeface="normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView4"
app:layout_constraintTop_toTopOf="@+id/imageView4"
app:layout_constraintHorizontal_bias="0.0" />
<TextView
android:id="@+id/textView8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:fontFamily="@font/source_sans"
android:text="Small Text"
android:textColor="@color/primary_text"
android:textSize="24sp"
android:typeface="normal"
app:layout_constraintTop_toTopOf="@+id/imageView3"
app:layout_constraintStart_toEndOf="@+id/imageView3"
android:layout_marginStart="16dp" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:1)
如果您将android:layout_width
中的android:layout_height
和ImageView
更改为常量尺寸,我可以这样做,例如48dp
代替wrap_content
然后在TextView
添加android:minHeight="48dp"
。下面添加了一个工作示例和xml:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="@mipmap/ic_launcher"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:layout_marginLeft="16dp"
android:text="Lorem ipsum"
app:layout_constraintLeft_toRightOf="@+id/imageView"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintTop_toTopOf="@+id/imageView" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="@mipmap/ic_launcher"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
android:layout_marginLeft="0dp"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:minHeight="48dp"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. "
app:layout_constraintLeft_toRightOf="@+id/imageView2"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintTop_toTopOf="@+id/imageView2" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="@mipmap/ic_launcher"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. "
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="0dp"
app:layout_constraintLeft_toRightOf="@+id/imageView4"
android:layout_marginLeft="16dp"
app:layout_constraintTop_toTopOf="@+id/imageView4"
android:layout_marginTop="0dp" />
</android.support.constraint.ConstraintLayout>
答案 1 :(得分:0)
我发布的信息这个答案将有助于将来参考 我认为这是 wrap_content 数据处理的 ConstarintLayout 问题,现在很容易从新的 ConstraintLayout 版本中获取 beta <强>
它引入了新功能 ,我们可以解决这个 wrap_content 问题,@ kazhiu的回答也会在这里解决,因为 ImageView 如果ImageView具有wrap_content或动态高度,它将无法工作,因此在这种情况下 constraint.Barrier 非常有用。 设置投注ContraintLayout我们必须做以下
在项目gradle文件中添加maven支持,如下所示
allprojects {
repositories {
maven { url 'https://maven.google.com' }
jcenter()
}
}
然后在app gardle依赖项中添加ConstarintLayout库依赖项
compile 'com.android.support.constraint:constraint-layout:1.1.0-beta1'
这是使用constraint.Barrier
的@ kazhiu代码<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="@mipmap/ic_launcher"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="Lorem ipsum"
app:layout_constraintLeft_toRightOf="@+id/imageView"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintTop_toTopOf="@+id/imageView" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="@mipmap/ic_launcher"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/barrierone"
android:layout_marginLeft="0dp"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. "
app:layout_constraintLeft_toRightOf="@+id/imageView2"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintTop_toTopOf="@+id/imageView2" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="@mipmap/ic_launcher"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/barrierone2" />
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. "
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="0dp"
app:layout_constraintLeft_toRightOf="@+id/imageView4"
android:layout_marginLeft="16dp"
app:layout_constraintTop_toTopOf="@+id/imageView4"
android:layout_marginTop="0dp" />
<android.support.constraint.Barrier
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/barrierone"
app:layout_constraintTop_toBottomOf="@+id/imageView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:constraint_referenced_ids="imageView,textView"
app:barrierDirection="bottom" />
<android.support.constraint.Barrier
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/barrierone2"
app:layout_constraintTop_toBottomOf="@+id/imageView2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:constraint_referenced_ids="imageView2,textView2"
app:barrierDirection="bottom" />
</android.support.constraint.ConstraintLayout>
设计和蓝图
android.support.constraint.Barrier 中的
app:constraint_referenced_ids="id1,id2"
将包含其他视图顶部的wrap_content视图的id,我们也可以通过将height替换为match_constraint(0dp)和width to wrap_content来创建水平屏障