简体   繁体   English

约束布局左右约束

[英]constraint layout left and right constraint

I have a constraint layout and when the string is long enough it overlaps the element on the right.我有一个约束布局,当字符串足够长时,它会与右侧的元素重叠。 Sample code is:示例代码是:

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

 ...

在此处输入图片说明

Then I tried to nameTv to add right constraint to left of the element on the right like this app:layout_constraintRight_toLeftOf="@id/priceFl" and this happened:然后我尝试 nameTv 向右侧元素的左侧添加右侧约束,就像这个app:layout_constraintRight_toLeftOf="@id/priceFl"这样发生了:

在此处输入图片说明

Is there a way to position this text between icon and price views?有没有办法在图标和价格视图之间定位此文本?

Try this试试这个

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

Try this, the width is 0dp and using app:layout_constraintHorizontal_weight you can adjust the width as per your requirement.试试这个,宽度是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>

I prefer using constraintStart and constraintEnd instead of constraintLeft and constraintRight , just a personal choice.我更喜欢使用constraintStartconstraintEnd而不是constraintLeftconstraintRight ,这只是个人选择。

try this :试试这个:

    <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 与图像和价格的左侧和右侧对齐。

You can use this (also add paddings or margins according your design)您可以使用它(也可以根据您的设计添加填充或边距)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM