Android 约束布局,带权重的视图?

SSP*_*SSP 2 xml android android-layout android-constraintlayout

所以像下图一样,我希望视图 B 的长度是视图 A 的两倍。

在此处输入图片说明

我真正需要的是视图 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>
Run Code Online (Sandbox Code Playgroud)

Tam*_*bul 5

您可以使用app:layout_constraintWidth_percentti 告诉您的视图如何在屏幕上展开。

对于您想要的外观,您可以像这样使用它:

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


<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/button2"
    app:layout_constraintWidth_percent=".33"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintWidth_percent=".66"
    app:layout_constraintStart_toEndOf="@+id/button"
    app:layout_constraintTop_toTopOf="@+id/button" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

它看起来像这样

在此处输入图片说明