约束布局左右约束

时间:2018-01-22 08:56:25

标签: android layout android-constraintlayout

我有一个约束布局,当字符串足够长时,它会重叠右边的元素。示例代码为:

  <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>

 ...

enter image description here

然后我尝试将nameTv添加到右侧元素的左侧,如app:layout_constraintRight_toLeftOf="@id/priceFl",这就发生了:

enter image description here

有没有办法在图标和价格视图之间定位此文字?

5 个答案:

答案 0 :(得分:5)

试试这个

<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"/>

答案 1 :(得分:2)

"3"

答案 2 :(得分:0)

试试这个:

    <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"/>

答案 3 :(得分:0)

尝试使用固定长度&#34; layout_width&#34;而不是&#34; wrap_content&#34;这将解决您的问题。但是我建议您分别将TextView对齐图像和价格的左右两边。

答案 4 :(得分:0)

你可以使用它(也可以根据你的设计添加填充或边距)

<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"/>
相关问题