Android 如何在父垂直线性布局中隔开垂直线性布局,使其填充屏幕?

Android 如何在父垂直线性布局中隔开垂直线性布局,使其填充屏幕?,android,android-linearlayout,android-view,spacing,android-layout-weight,Android,Android Linearlayout,Android View,Spacing,Android Layout Weight,我目前的布局如下: 高亮显示的布局是一个线性布局,其中包含三个额外的水平线性布局(每个文本+seekbar组合一个) 我想突出显示的线性布局,以扩大和填补屏幕的空间,使“保存”按钮在屏幕底部 我尝试过使用android:layout_weight并将相同的权重分配给高亮显示的布局的每个子级,但似乎没有什么不同 以下是我的布局xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http

我目前的布局如下:

高亮显示的布局是一个线性布局,其中包含三个额外的水平线性布局(每个文本+seekbar组合一个)

我想突出显示的线性布局,以扩大和填补屏幕的空间,使“保存”按钮在屏幕底部

我尝试过使用android:layout_weight并将相同的权重分配给高亮显示的布局的每个子级,但似乎没有什么不同

以下是我的布局xml:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/because_text"
            style="@style/TextAppearance.AppCompat.Subhead"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:text="@string/because" />

        <EditText
            android:id="@+id/moodCommentEditText"
            style="@style/Base.TextAppearance.AppCompat.Body1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:hint="@string/add_a_comment_here" />

    </LinearLayout>

<!-- HIGHLIGHTED LAYOUT -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="3"
        android:orientation="vertical"
        android:paddingEnd="@dimen/activity_vertical_margin"
        android:paddingStart="@dimen/activity_vertical_margin">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/happy_text"
                style="@style/TextAppearance.AppCompat.Subhead"
                tools:text="Unhappy"
                android:layout_width="65sp"
                android:layout_height="wrap_content"
                android:maxLines="1" />

            <SeekBar
                android:id="@+id/happiness_seekbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:max="100"
                android:theme="@style/happinessSeekbarTheme" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <TextView
                android:id="@+id/stress_text"
                style="@style/TextAppearance.AppCompat.Subhead"
                android:layout_width="65sp"
                android:layout_height="wrap_content"

                android:maxLines="1"
                android:text="@string/stress" />

            <SeekBar
                android:id="@+id/stress_seekbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:max="100"
                android:theme="@style/stressSeekbarTheme" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <TextView
                android:id="@+id/pain_text"
                style="@style/TextAppearance.AppCompat.Subhead"
                android:layout_width="65sp"
                android:layout_height="wrap_content"
                android:maxLines="1"
                android:text="@string/pain" />

            <SeekBar
                android:id="@+id/pain_seekbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:max="100"
                android:maxHeight="3dp"
                android:theme="@style/painSeekbarTheme" />

        </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/saveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:text="@string/save"
        android:textColor="@color/colorPrimary"
        android:textSize="15dp" />
</LinearLayout>

尝试将最顶端的
线性布局设置为
android:layout\u height
匹配父项
——当前设置为
wrap\u content
,这意味着它将收缩到允许正确渲染其子级的最小可能高度。

尝试将最顶端的
线性布局设置为android:layout\u height
match\u parent
-它当前设置为
wrap\u content
,这意味着它将收缩到尽可能小的高度,以便正确地渲染其子对象。


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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="@dimen/activity_horizontal_margin"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/because_text"
        style="@style/TextAppearance.AppCompat.Subhead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:text="@string/because" />

    <EditText
        android:id="@+id/moodCommentEditText"
        style="@style/Base.TextAppearance.AppCompat.Body1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:hint="@string/add_a_comment_here" />

</LinearLayout>

<!-- HIGHLIGHTED LAYOUT -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="vertical"
    android:paddingEnd="@dimen/activity_vertical_margin"
    android:paddingStart="@dimen/activity_vertical_margin">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/happy_text"
            style="@style/TextAppearance.AppCompat.Subhead"
            tools:text="Unhappy"
            android:layout_width="65sp"
            android:layout_height="wrap_content"
            android:maxLines="1" />

        <SeekBar
            android:id="@+id/happiness_seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:theme="@style/happinessSeekbarTheme" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/stress_text"
            style="@style/TextAppearance.AppCompat.Subhead"
            android:layout_width="65sp"
            android:layout_height="wrap_content"

            android:maxLines="1"
            android:text="@string/stress" />

        <SeekBar
            android:id="@+id/stress_seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:theme="@style/stressSeekbarTheme" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/pain_text"
            style="@style/TextAppearance.AppCompat.Subhead"
            android:layout_width="65sp"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:text="@string/pain" />

        <SeekBar
            android:id="@+id/pain_seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:maxHeight="3dp"
            android:theme="@style/painSeekbarTheme" />

    </LinearLayout>

</LinearLayout>

<Button
    android:id="@+id/saveButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@android:color/transparent"
    android:text="@string/save"
    android:textColor="@color/colorPrimary"
    android:textSize="15sp" />
</LinearLayout>

您可以使用辅助空间视图解决此问题

添加一个带有权重的空视图,以便将线性布局中的以下视图强制放置在末端

注意:Android有一个特殊的视图用于此目的,但它只在API级别14中出现

像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/add_mood_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" <!-- Also changed this -->
    android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="@dimen/activity_horizontal_margin"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/because_text"
        style="@style/TextAppearance.AppCompat.Subhead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:text="@string/because" />

    <EditText
        android:id="@+id/moodCommentEditText"
        style="@style/Base.TextAppearance.AppCompat.Body1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:hint="@string/add_a_comment_here" />

</LinearLayout>

<!-- HIGHLIGHTED LAYOUT -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="3"
    android:orientation="vertical"
    android:paddingEnd="@dimen/activity_vertical_margin"
    android:paddingStart="@dimen/activity_vertical_margin">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/happy_text"
            style="@style/TextAppearance.AppCompat.Subhead"
            tools:text="Unhappy"
            android:layout_width="65sp"
            android:layout_height="wrap_content"
            android:maxLines="1" />

        <SeekBar
            android:id="@+id/happiness_seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:theme="@style/happinessSeekbarTheme" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/stress_text"
            style="@style/TextAppearance.AppCompat.Subhead"
            android:layout_width="65sp"
            android:layout_height="wrap_content"

            android:maxLines="1"
            android:text="@string/stress" />

        <SeekBar
            android:id="@+id/stress_seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:theme="@style/stressSeekbarTheme" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/pain_text"
            style="@style/TextAppearance.AppCompat.Subhead"
            android:layout_width="65sp"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:text="@string/pain" />

        <SeekBar
            android:id="@+id/pain_seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:maxHeight="3dp"
            android:theme="@style/painSeekbarTheme" />

    </LinearLayout>

</LinearLayout>

<!-- Space View: Add this view here for white space -->
<View
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1"/>

<Button
    android:id="@+id/saveButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@android:color/transparent"
    android:text="@string/save"
    android:textColor="@color/colorPrimary"
    android:textSize="15dp" />
</LinearLayout>

android:orientation=“垂直”

您可以使用辅助空间视图解决此问题

添加一个带有权重的空视图,以便将线性布局中的以下视图强制放置在末端

注意:Android有一个特殊的视图用于此目的,但它只在API级别14中出现

像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/add_mood_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" <!-- Also changed this -->
    android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="@dimen/activity_horizontal_margin"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/because_text"
        style="@style/TextAppearance.AppCompat.Subhead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:text="@string/because" />

    <EditText
        android:id="@+id/moodCommentEditText"
        style="@style/Base.TextAppearance.AppCompat.Body1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:hint="@string/add_a_comment_here" />

</LinearLayout>

<!-- HIGHLIGHTED LAYOUT -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="3"
    android:orientation="vertical"
    android:paddingEnd="@dimen/activity_vertical_margin"
    android:paddingStart="@dimen/activity_vertical_margin">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/happy_text"
            style="@style/TextAppearance.AppCompat.Subhead"
            tools:text="Unhappy"
            android:layout_width="65sp"
            android:layout_height="wrap_content"
            android:maxLines="1" />

        <SeekBar
            android:id="@+id/happiness_seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:theme="@style/happinessSeekbarTheme" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/stress_text"
            style="@style/TextAppearance.AppCompat.Subhead"
            android:layout_width="65sp"
            android:layout_height="wrap_content"

            android:maxLines="1"
            android:text="@string/stress" />

        <SeekBar
            android:id="@+id/stress_seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:theme="@style/stressSeekbarTheme" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/pain_text"
            style="@style/TextAppearance.AppCompat.Subhead"
            android:layout_width="65sp"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:text="@string/pain" />

        <SeekBar
            android:id="@+id/pain_seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:maxHeight="3dp"
            android:theme="@style/painSeekbarTheme" />

    </LinearLayout>

</LinearLayout>

<!-- Space View: Add this view here for white space -->
<View
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1"/>

<Button
    android:id="@+id/saveButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@android:color/transparent"
    android:text="@string/save"
    android:textColor="@color/colorPrimary"
    android:textSize="15dp" />
</LinearLayout>

android:orientation=“垂直”

您是否尝试使用RelativeLayout?@Dammeman预期输出将有助于提供更好的结果answer@Goran我想避免使用RelativeLayout,因为据我所知,分隔事物的唯一方法是使用边距和填充,这可能会导致不同屏幕大小出现不可预测的行为。您是否尝试使用RelativeLayout?@Dammeman expected output将有助于提供更好的性能answer@Goran我想避免使用RelativeLayout,因为据我所知,分隔事物的唯一方法是使用边距和填充,这可能会导致不同屏幕尺寸的不可预测行为。