layout_constraintLeft_toLeftOf和layout_constraintRight_toRightOf不会使视图像约束一样宽

时间:2017-08-02 10:56:51

标签: android android-layout android-constraintlayout

我的示例中有两个小部件(TextView)。第一个小部件具有固定宽度,第二个小部件应该与第一个小部件一样宽。

请考虑以下代码段:

RelativeLayout的。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="192dp"
        android:layout_height="wrap_content"
        android:background="#ccc"
        android:text="Short message" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/textView"
        android:layout_alignRight="@id/textView"
        android:layout_below="@id/textView"
        android:layout_marginTop="16dp"
        android:background="#aaa"
        android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book" />
</RelativeLayout>

enter image description here

ConstraintLayout。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="192dp"
        android:layout_height="wrap_content"
        android:background="#ccc"
        android:text="Short message" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp"
        android:background="#aaa"
        android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book"
        app:layout_constraintLeft_toLeftOf="@+id/textView"
        app:layout_constraintRight_toRightOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintHorizontal_bias="0.49" />
</android.support.constraint.ConstraintLayout>

enter image description here

正如您在第二张图片中看到的那样,第二个小部件不像第一个小部件那么宽。为什么?我认为app:layout_constraintLeft_toLeftOf="@+id/textView" app:layout_constraintRight_toRightOf="@+id/textView"可以解决问题。

2 个答案:

答案 0 :(得分:3)

按如下方式更改第二个TextView:

<TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:background="#aaa"
        android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book"
        app:layout_constraintLeft_toLeftOf="@+id/textView"
        app:layout_constraintRight_toRightOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintHorizontal_bias="0.486" />

set layout_width = 0匹配约束,如果要对齐textView

,则不应设置边距
android:layout_width="0dp" 

答案 1 :(得分:2)

尝试将android:layout_width设置为0dp 和app:layout_constraintWidth_default="wrap"。同时删除左右边距。