所以像下面的图片一样,我希望视图B的长度是视图A的两倍。
android - Android约束布局, View 有重量吗?-LMLPHP
我真正需要的是视图A粘在左边缘,视图B粘在右边缘,它们在中间粘在一起。在长度上,B的长度是A的两倍。我试了很多次,但是没有运气!最后,有些东西不是我想要的。视图与其相应边之间或两个视图之间存在不需要的空间。
这是我最后一次尝试:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity">

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_marginTop="36dp"
    android:ems="10"
    android:text="B"
    android:inputType="textPersonName"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_weight="2"
    app:layout_constraintStart_toEndOf="@+id/textView"
    app:layout_constraintTop_toTopOf="parent" />


<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_marginTop="36dp"
    android:gravity="center"
    android:text="A"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

最佳答案

要使用layout_constraintHorizontal_weight,必须先将Views放入链中。这意味着您必须将Views相互约束,并将父对象水平约束。在您的案例中,您缺少的是app:layout_constraintEnd_toStartOf="@id/editText"上的TextView。您还需要将android:layout_widthViews0dp更改为,以便强制执行权重:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginTop="36dp"
        android:ems="10"
        android:text="B"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_weight="2"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginTop="36dp"
        android:gravity="center"
        android:text="A"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/editText"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

10-08 07:49
查看更多