【问题标题】:constraint layout left and right constraint约束布局左右约束
【发布时间】:2020-03-25 08:59:52
【问题描述】:

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

  <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" 将右侧约束添加到右侧元素的左侧,这发生了:

有没有办法将此文本放置在图标视图和价格视图之间?

【问题讨论】:

  • 这里可以加xml代码吗
  • @MayankPanchal,你能再看看吗?

标签: android layout android-constraintlayout


【解决方案1】:

试试这个

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

【讨论】:

  • 仅供参考,如果您使用此 sn-p 而不将宽度设置为 0dp 它将居中
【解决方案2】:

试试这个,宽度是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,这只是个人选择。

【讨论】:

    【解决方案3】:

    试试这个:

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

    【讨论】:

      【解决方案4】:

      尝试使用固定长度的“layout_width”而不是“wrap_content”,这将解决您的问题。不过,我建议您将 TextView 分别与图像和价格的左右对齐。

      【讨论】:

        【解决方案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"/>
        

        【讨论】:

          猜你喜欢
          • 2021-04-29
          • 1970-01-01
          • 1970-01-01
          • 2018-12-12
          • 2022-11-02
          • 1970-01-01
          • 1970-01-01
          • 2020-05-29
          • 1970-01-01
          相关资源
          最近更新 更多