【问题标题】:Convert this LinearLayout to RelativeLayout and ConstraintLayout [closed]将此 LinearLayout 转换为 RelativeLayout 和 ConstraintLayout [关闭]
【发布时间】:2021-08-01 06:23:00
【问题描述】:

我使用嵌套的LinearLayouts 创建了以下布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/id1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="foo1"
            android:layout_weight="1"
            />

        <TextView
            android:id="@+id/id_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="foo_value"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/id2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="foo2"
            android:layout_weight="1"
            />

        <TextView
            android:id="@+id/id_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="foo2_value"
            />
    </LinearLayout>



</LinearLayout>  

这给出了以下内容:

a) 如何将此布局转换为使用 RelativeLayout 并获得相同的结果? 我尝试了以下方法:

<RelativeLayout   
    android:layout_width="match_parent"
    android:layout_height="match_parent">    
    <TextView
        android:id="@+id/id1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="foo1"
        android:layout_alignParentStart="true"
        />

    <TextView
        android:id="@+id/id_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="foo_value"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@id/id1"

        />

    <TextView
        android:id="@+id/id2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="foo2"
        android:layout_below="@id/id1"
        android:layout_alignParentStart="true"
        />

    <TextView
        android:id="@+id/id_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="foo2_value"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@id/id2"
        android:layout_below="@id/id_1"
        />
</RelativeLayout>   

但结果是:

我做错了什么?

b) 我如何转换为ConstraintLayout?我应该使用什么?

【问题讨论】:

    标签: android android-linearlayout android-view android-relativelayout android-constraintlayout


    【解决方案1】:

    a) 相对布局

    <?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/id1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_toStartOf="@+id/id_1"
        android:text="foo1" />
    
    <TextView
        android:id="@+id/id_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:text="foo_value" />
    
    <TextView
        android:id="@+id/id2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/id1"
        android:layout_alignParentStart="true"
        android:layout_toStartOf="@+id/id_2"
        android:text="foo2" />
    
    <TextView
        android:id="@+id/id_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/id_1"
        android:layout_alignParentEnd="true"
        android:text="foo2_value" />
    </RelativeLayout>
    

    b) 约束布局

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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/id1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="foo1"
        app:layout_constraintEnd_toStartOf="@+id/id_1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
        android:id="@+id/id_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="foo_value"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
        android:id="@+id/id2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="foo2"
        app:layout_constraintEnd_toStartOf="@+id/id_2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id1" />
    
    <TextView
        android:id="@+id/id_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="foo2_value"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id1" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 当我添加 android:layout_toStartOf="@+id/id_1" 时,一切都消失了
    • 另外你在TextView中使用android:layout_width="match_parent"
    • 我也复制/粘贴了ConstraintLayout,它根本没有对齐
    • @Jim 尝试将ConstraintLayoutRelativeLayout 的完整sn-p 粘贴到单独的布局文件中。我已将它们编辑为完整的 XML,并将 TextView 更改回 wrap_content。
    • 所以解决方案是将RelativeLayout的第一个与第二个的开始对齐?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 2016-09-16
    • 2020-04-14
    • 2023-03-18
    相关资源
    最近更新 更多