首页 > 解决方案 > 使用 ScrollView 时,RelativeLayout 按钮消失

问题描述

我的布局类似于本示例中使用的 on

在此处输入图像描述

唯一的区别是我想在 ScrollView 之后添加 3 个按钮。不幸的是,一旦我添加了 ScrollView,我的按钮就从屏幕上消失了。(当我有一个没有 ScrollView 的 TextView 时很好)。

我怎样才能让我的按钮回到屏幕底部?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/article_heading"
        android:background="@color/colorPrimary"
        android:textColor="@android:color/white"
        android:padding="@dimen/padding_regular"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
        android:textStyle="bold"
        android:text="@string/article_title"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/article_subheading"
        android:layout_below="@id/article_heading"
        android:padding="@dimen/padding_regular"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault"
        android:text="@string/article_subtitle"/>

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/article_scrollview"
        android:layout_below="@id/article_subheading">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingExtra="@dimen/line_spacing"
            android:id="@+id/article"
            android:padding="@dimen/padding_regular"
            android:textAppearance="@android:style/TextAppearance.DeviceDefault"
            android:autoLink="web"
            android:text="@string/article_text"/>

    </ScrollView>

    <Button
        android:id="@+id/butConnect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/article_scrollview"
        android:layout_marginStart="@dimen/button_margin"
        android:onClick="onButLoginClicked"
        android:text="@string/but_text_connect"/>

    <Button
        android:id="@+id/butAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/butConnect"
        android:layout_marginStart="@dimen/button_margin"
        android:text="@string/but_text_add" />

    <Button
        android:id="@+id/butSale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/butAdd"
        android:layout_marginStart="@dimen/button_margin"
        android:text="@string/but_text_sale" />

</RelativeLayout>

标签: androidandroid-scrollviewandroid-relativelayout

解决方案


仔细设计您的布局并遵循设计原则 遵循https://developer.android.com/design

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/article_heading"
    android:background="@color/colorPrimary"
    android:textColor="@android:color/white"
    android:padding="@dimen/padding_regular"
    android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
    android:textStyle="bold"
    android:text="@string/article_title"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/article_subheading"
    android:layout_below="@id/article_heading"
    android:padding="@dimen/padding_regular"
    android:textAppearance="@android:style/TextAppearance.DeviceDefault"
    android:text="@string/article_subtitle"/>

<ScrollView
    android:layout_above="@+id/butConnect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/article_scrollview"
    android:layout_below="@id/article_subheading">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lineSpacingExtra="@dimen/line_spacing"
        android:id="@+id/article"
        android:padding="@dimen/padding_regular"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault"
        android:autoLink="web"
        android:text="@string/article_text"/>

</ScrollView>

<Button
    android:id="@+id/butConnect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/butAdd"
    android:layout_marginStart="@dimen/button_margin"
    android:onClick="onButLoginClicked"
    android:text="@string/but_text_connect"/>

<Button
    android:id="@+id/butAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/butSale"
    android:layout_marginStart="@dimen/button_margin"
    android:text="@string/but_text_add" />

<Button
    android:id="@+id/butSale"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="@dimen/button_margin"
    android:text="@string/but_text_sale" />

</RelativeLayout>

推荐阅读