【问题标题】:Android Constraint layout, views with weight?Android约束布局,重量视图?
【发布时间】:2023-04-07 19:10:02
【问题描述】:

如下图所示,我希望视图 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>

【问题讨论】:

    标签: android xml android-layout android-constraintlayout


    【解决方案1】:

    您可以使用app:layout_constraintWidth_percent 告诉您的视图如何在屏幕上传播。

    为了您想要的外观,您可以这样使用它:

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

    看起来像这样

    【讨论】:

    • 您的答案也有效,除了这样看起来父填充被忽略,这并不酷。无论如何,谢谢。
    【解决方案2】:

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

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

    【讨论】:

    • 谢谢,所以我将它们添加到水平链中,效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多