[英]constraint layout left and right constraint
我有一个约束布局,当字符串足够长时,它会与右侧的元素重叠。 示例代码是:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iconIv"
android:layout_width="36dp"
android:layout_height="36dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/nameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@id/iconIv"
app:layout_constraintTop_toTopOf="parent"/>
<FrameLayout
android:id="@+id/priceFl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
...
然后我尝试 nameTv 向右侧元素的左侧添加右侧约束,就像这个app:layout_constraintRight_toLeftOf="@id/priceFl"
这样发生了:
有没有办法在图标和价格视图之间定位此文本?
试试这个
<TextView
android:id="@+id/nameTv"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@id/iconIv"
app:layout_constraintRight_toLeftOf="@+id/priceFl"
app:layout_constraintTop_toTopOf="parent"/>
试试这个,宽度是0dp
并且使用app:layout_constraintHorizontal_weight
你可以根据你的要求调整宽度。
<ImageView
android:id="@+id/iconIv"
android:layout_width="36dp"
android:layout_height="36dp"
android:src="@mipmap/ic_launcher_round"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/nameTv"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/nameTv"
android:layout_width="0dp"
app:layout_constraintHorizontal_weight="2"
android:layout_height="wrap_content"
android:text="Some text"
app:layout_constraintStart_toEndOf="@+id/iconIv"
app:layout_constraintEnd_toStartOf="@+id/priceFl"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/priceFl"
android:layout_width="0dp"
app:layout_constraintHorizontal_weight="1"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/nameTv"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
我更喜欢使用constraintStart
和constraintEnd
而不是constraintLeft
和constraintRight
,这只是个人选择。
试试这个:
<TextView
android:id="@+id/nameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="52dp" //change here the distance between text and icon
app:layout_constraintLeft_toRightOf="@id/iconIv"
app:layout_constraintRight_toLeftOf="@id/priceFl"
app:layout_constraintTop_toTopOf="parent"/>
尝试使用固定长度的“layout_width”而不是“wrap_content”,这将解决您的问题。但是我建议您分别将 TextView 与图像和价格的左侧和右侧对齐。
您可以使用它(也可以根据您的设计添加填充或边距)
<TextView
android:id="@+id/nameTv"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@id/iconIv"
app:layout_constraintRight_toLeftOf="@id/priceFl"
app:layout_constraintTop_toTopOf="parent"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.