【问题标题】:align two overlapping linear layouts like two rows对齐两个重叠的线性布局,如两行
【发布时间】:2021-09-06 05:59:30
【问题描述】:

我在一个相对布局中有两个线性布局。

无论我做什么,这两个线性布局都会相互重叠。

我想将它们排列成不重叠的行顺序(一个在另一个之下)。

如果线性布局有这么多问题,我正在考虑使用表格布局,但想在这里问一下线性布局是否有一些好处。

<?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">
<LinearLayout
    android:id="@+id/linearlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="1">
    <!--This layout only holds a TextView
            inside a CardView-->
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:text="this is another test this is another test"
        android:textColor="#0C0909"
        android:textSize="20dp" />
</LinearLayout>
    <LinearLayout
        android:id="@+id/linear_one"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true">

        <ImageView
            android:id="@+id/image_one"
            android:layout_width="match_parent"
            android:src="@mipmap/nvoids_logo_foreground"
            android:layout_height="100dp"/>
    </LinearLayout>
</RelativeLayout>

【问题讨论】:

  • 为什么不使用约束布局?
  • @Raghunandan 是哪一个?用 ConstraintLayout 替换 RelativeLayout 或 LinearLayout?
  • 是的,使用一个约束布局

标签: android android-layout layout


【解决方案1】:

在您的线性布局中使用高度 wrap_content 而不是 match_parent。并在您的第二个线性布局中添加 android:layout_below="@id/linearlayout"。

如下更改。

<?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">
    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <!--This layout only holds a TextView
                inside a CardView-->
        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="20dp"
            android:text="this is another test this is another test"
            android:textColor="#0C0909"
            android:textSize="20dp" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linear_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@id/linearlayout">

        <ImageView
            android:id="@+id/image_one"
            android:layout_width="match_parent"
            android:src="@mipmap/nvoids_logo_foreground"
            android:layout_height="100dp"/>
    </LinearLayout>
</RelativeLayout>

【讨论】:

    【解决方案2】:

    避免深层视图层次结构,使用约束布局以获得更好的性能。

    <?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/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:text="this is another test this is another test"
            android:textColor="#0C0909"
            android:textSize="20dp"
    
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>
    
        <ImageView
            android:id="@+id/image_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_expand_more"
    
            app:layout_constraintTop_toBottomOf="@id/text"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
    
            android:layout_margin="32dp"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 我认为谷歌在帮助,但不是对齐语句,而是为了设计一个地狱,他们想出了 layout_constraintTop_toBottomOf , layout_constraintStart_toStartOf , layout_constraintEnd_toEndOf 等等等等等等等等。疯狂的东西......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 2015-10-15
    • 2022-08-18
    相关资源
    最近更新 更多