LinearLayout 适用于另一个 LinearLayout

问题描述

我在 constrainLayout 中有两个线性布局, 底部线性布局包括按钮,其高度为 wrap_content 并且必须换行,因为有时我只保留 1 个按钮,有时保留 2 个按钮。

我想将顶部 linearLayout 的高度设置为 max 但不覆盖第二个布局。 所以我决定将 match_parent 设置为第一个,但之后它覆盖了第二个布局。

  • 我不能使用权重/权重

      <androidx.constraintlayout.widget.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"
          tools:context=".MainActivity">
    
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              app:layout_constraintBottom_toTopOf="@+id/linearLayout"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintHorizontal_bias="1.0"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent">
    
          </LinearLayout>
    
          <LinearLayout
              android:id="@+id/linearLayout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:padding="16dp"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent">
    
              <Button
                  android:id="@+id/button"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="Button1" />
    
              <Button
                  android:id="@+id/button2"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="Button2"
                  android:visibility="gone" />
          </LinearLayout>
    
      </androidx.constraintlayout.widget.ConstraintLayout>
    

解决方法

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="11"
    android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="9"
    app:layout_constraintBottom_toTopOf="@+id/linearLayout"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="1"
    android:padding="16dp"
    android:weightSum="2"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button2"
        " />
</LinearLayout>

试试这个增加的权重总和和权重来按比例布局

,

由于使用的是ConstraintLayout,所以可以将第一个LinearLayout的高度设置为0dp。 ConstraintLayout 将其解释为“匹配约束”。 下一步是正确定义第一个 LinearLayout(您所做的)的约束。

您可以阅读有关 ConstraintLayout here 的更多信息。

  <androidx.constraintlayout.widget.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"
      tools:context=".MainActivity">

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:orientation="vertical"
          app:layout_constraintBottom_toTopOf="@+id/linearLayout"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="1.0"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent">

      </LinearLayout>

      <LinearLayout
          android:id="@+id/linearLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:padding="16dp"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent">

          <Button
              android:id="@+id/button"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="Button1" />

          <Button
              android:id="@+id/button2"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="Button2"
              android:visibility="gone" />
      </LinearLayout>

  </androidx.constraintlayout.widget.ConstraintLayout>
,

要做到这一点,您必须使用线性布局的组合作为父级和属性 layoutWeight ,如果您对顶部布局和按钮的容器使用 layoutWeight 的常量值,那么这将导致在不同屏幕高度设备上出现一些问题,而不是此解决方案使您能够通过将其容器的高度设置为 wrapContent 并为布局内容提供所有顶部区域来将按钮高度设置为足够大。 您还可以使顶部的 linearLayout 可滚动以获得最佳体验:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/this_layout_will_fil_all_space_except_the_bottom_layout_space"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:background="#000000"
        android:layout_weight="1">

    </LinearLayout>
    <LinearLayout
        android:id="@+id/button_layout_at_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:padding="10dp">
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1" 
            android:layout_marginEnd="50dp"/>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2"
            android:visibility="visible" />

    </LinearLayout>

</LinearLayout>

对于 Buttons ,您可以将其宽度设置为 wrapContent 并将重力属性添加到其父级,如上所示,这样即使一个按钮消失了,另一个也将完美地适合容器的中心。

享受吧!

,

使用 ConstraintLayout 的主要优点是避免使用任何其他嵌套的 ViewGroups,您在布局中添加的层次结构越多,性能就越低。您可以check documentation了解更多。

因此,为了提高性能,最好直接在 ConstraintLayout 中添加小部件/视图。

所以,这里我去掉了底部的 LinearLayout,只保留了直接在 ConstraintLayout 内的按钮,你可以对顶部的 LinearLayout 做同样的事情,你可以添加任何底层直接查看 ConstraintLayout

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="Button1"
        app:layout_constraintBottom_toTopOf="@id/button2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:text="Button2"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...