[英]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.