我在我的应用程序中使用 Activity 和 Fragment 作为首选项屏幕,按照 Google's recommended format 。我想知道如何让此 Activity 中的操作栏显示“设置”。另外,如何包含后退箭头以返回调用 Activity ?这是 Android 中的内置函数,还是我需要一些额外的可绘制资源用于箭头?
目前,使用以下代码我收到错误 android.view.InflateException: Binary XML file line #9: Error inflateing class android.support.v7.widget.Toolbar
这是我的偏好 Activity 和 Fragment:
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_settings);
//setupActionBar();
// Display the fragment as the main content.
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
super.onActivityCreated(savedInstanceState);
// Load the preferences from an XML resource
setPreferencesFromResource(R.xml.preferences, rootKey);
Toolbar toolbar = (Toolbar) getView().findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
}
}
}
这是我的偏好设置.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="@string/settings_title"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<PreferenceCategory
android:key="settings_game_mode"
android:title="@string/settings_game_mode">
<ListPreference
android:key="game_mode_list"
android:title="Select Rule Mode"
android:summary="%s"
android:entries="@array/game_mode_list_entries"
android:entryValues="@array/game_mode_list_entries_values"
android:defaultValue="FIRST_TO_21" />
</PreferenceCategory>
</PreferenceScreen>
这是我的 SettingsActivity 使用的样式:
<style name="SettingsTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
</style>
最佳答案
您的设置存在一些错误:
- 如果您使用
Toolbar
设置自己的应用栏,则需要使用主题的NoActionBar
变体 工具栏
不能成为您的preferences.xml
的一部分,因为它严格用于首选项PreferenceScreen
没有layout_width
和layout_height
属性,因为它不是布局元素
主要问题是您正在将 android.R.id.content
替换为首选项 fragment 。这意味着您无法使用自定义布局元素,因此您有两个选择:
选项 1 - 正常布局
使用通过 setContentView(...)
设置的正常布局。此布局应包含该 fragment 的 Toolbar
和一个容器(例如 FrameLayout
),然后用首选项 fragment “替换”该容器。通过调用setSupportActionBar(...)
可以将工具栏
设置为操作栏。请注意,在这种情况下,您必须使用 Theme.AppCompat.Light.NoActionBar
作为主题的父主题。
选项 2 - 替换 android.R.id.content
如果您想坚持使用基本内容容器替换方法,则需要继续使用 Theme.AppCompat.Light.DarkActionBar
作为主题的父级,然后使用常用方法来控制工具栏:
- 在
SettingsActivity
的 list 中设置label
属性(或者,如果您想以编程方式设置它,请在onCreate(... )
) - 通过调用
setTitle(...)
获取ActionBar
- 您可以调用
getSupportActionBar()
在返回的实例上启用后退箭头
- 您可以调用
后退箭头点击可以在 Activity 的 onOptionsItemSelected(...)
中处理,例如:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Note that this should be handled manually only if you don't have a parent activity set up in the manifest. Check the Adding an Up Action guide for more details.
最后一点,让我向您介绍我的 setDisplayHomeAsUpEnabled(true)
这解决了您使用官方库可能遇到的很多问题。 support preferences v7 fix lib分支包含一个很好的自述文件(master 现在有点困惑),但是 master (基于 25.4.0)和 v26-beta (基于 26.0) .0-beta2)有示例应用程序(“app”文件夹),应该很容易复制/遵循以创建您自己的设置屏幕。示例应用程序向您展示了选项 1。
关于android - 如何在应用程序首选项屏幕的 ActionBar 中显示文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44992405/