Android - 在SettingsActivity屏幕底部包含一个ButtonBar?

问题描述 投票:1回答:1

我正在使用Preferences-API来创建我的设置屏幕。 截至目前,我的SettingsActivity布局XML包含一个简单的LinearLayout,里面有一个FrameLayout,我用它来包含我的Preferences片段。 但是,由于用户访问设置屏幕的方式,AppBar中没有“后退”按钮 - 因此,我想在“首选项”屏幕的底部显示一个ButtonBar,这样他们就可以点击一个cancel或者ok按钮..

这是我到目前为止尝试添加ButtonBar的内容:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SettingsActivity">

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<LinearLayout
    style="?android:attr/buttonBarStyle"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button style="?android:attr/buttonBarButtonStyle"
        android:id="@+id/btn_settingsActivity_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@android:string/cancel" />

    <Button style="?android:attr/buttonBarButtonStyle"
        android:id="@+id/btn_settingsActivity_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@android:string/ok" />

</LinearLayout>
</LinearLayout>

有谁知道我做错了什么? 使用上面的代码,FrameLayout仍然占据整个屏幕。 如果我给FrameLayout一个特定的height,我可以看到ButtonBar。 但是,我知道这不是我应该怎么做的。 有人可以帮忙解释我应该怎么做这个吗? 谢谢!

android android-fragments android-preferences
1个回答
2
投票

首先在您的孩子LinearLayout中添加一些方向,例如qazxsw poi并将高度改为qazxsw poi。

android:orientation="horizontal"

第二个告诉你的wrap_content你想通过重量属性占据剩余的空间,例如<LinearLayout style="?android:attr/buttonBarStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">.. 和性能目的将高度设置为零FrameLayout

android:layout_weight="1"

最终代码应如下所示:

android:layout_height="0dp"
© www.soinside.com 2019 - 2024. All rights reserved.