约束布局左右约束

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="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" 这将解决您的问题 problem.However 我建议您将 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"/>

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

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

我更喜欢使用 constraintStartconstraintEnd 而不是 constraintLeftconstraintRight,这只是个人选择。