有兩個小部件(在我的示例中爲TextView
s)。第一個小部件的寬度是固定的,第二個小部件應該和第一個小部件一樣寬。layout_constraintLeft_toLeftOf和layout_constraintRight_toRightOf不會使視圖像約束一樣寬
考慮下面的代碼片段:
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>
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>
正如你可以在第二圖片中看到,第二小部件不一樣寬,第一小。爲什麼?我以爲app:layout_constraintLeft_toLeftOf="@+id/textView" app:layout_constraintRight_toRightOf="@+id/textView"
會做到這一點。