Android - 在 SettingsActivity 屏幕底部包含一个 ButtonBar?
Android - Including a ButtonBar at bottom of SettingsActivity Screen?
我正在使用 Preferences-API
创建我的设置屏幕。
截至目前,我的 SettingsActivity
布局 XML 包含一个简单的 LinearLayout
,里面有一个 FrameLayout
,我用它来包含我的 Preferences
片段。
然而,由于用户访问设置屏幕的方式,AppBar 中没有 'back' 按钮 - 因此,我想显示一个 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
.
但是,我知道这不是我应该做的。
有人可以帮我解释一下我应该怎么做吗?
谢谢!
首先在您的子 LinerLayout 中添加一些方向,例如android:orientation="horizontal"
并将高度更改为 wrap_content
。
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">..
其次告诉你的 FrameLayout
你想通过权重 属性 占据剩余的 space 例如android:layout_weight="1"
并出于性能目的将高度设置为零 android:layout_height="0dp"
。
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
最终代码应如下所示:
<?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">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_settingsActivity_cancel"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@android:string/cancel" />
<Button
android:id="@+id/btn_settingsActivity_ok"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@android:string/ok" />
</LinearLayout>
</LinearLayout>
我正在使用 Preferences-API
创建我的设置屏幕。
截至目前,我的 SettingsActivity
布局 XML 包含一个简单的 LinearLayout
,里面有一个 FrameLayout
,我用它来包含我的 Preferences
片段。
然而,由于用户访问设置屏幕的方式,AppBar 中没有 'back' 按钮 - 因此,我想显示一个 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
.
但是,我知道这不是我应该做的。
有人可以帮我解释一下我应该怎么做吗?
谢谢!
首先在您的子 LinerLayout 中添加一些方向,例如android:orientation="horizontal"
并将高度更改为 wrap_content
。
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">..
其次告诉你的 FrameLayout
你想通过权重 属性 占据剩余的 space 例如android:layout_weight="1"
并出于性能目的将高度设置为零 android:layout_height="0dp"
。
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
最终代码应如下所示:
<?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">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_settingsActivity_cancel"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@android:string/cancel" />
<Button
android:id="@+id/btn_settingsActivity_ok"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@android:string/ok" />
</LinearLayout>
</LinearLayout>