如何在我的应用程序首选项屏幕中的ActionBar中显示文本?

时间:2017-07-09 02:23:21

标签: android android-layout android-fragments android-actionbar sharedpreferences

我根据Google's recommended format在我的应用中使用了Activity和Fragment作为首选项屏幕。我想知道如何在此活动中获取操作栏以说出“设置”。另外,如何包含后退箭头以返回调用活动?这是Android中的内置函数,还是我需要一些箭头的额外可绘制资源?

目前,使用以下代码我收到错误android.view.InflateException: Binary XML file line #9: Error inflating class android.support.v7.widget.Toolbar

以下是我的偏好活动和片段:

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>

2 个答案:

答案 0 :(得分:2)

您的设置存在一些错误:

  • 如果您使用Toolbar设置自己的appbar,则需要使用主题的NoActionBar变体
  • Toolbar不能成为您preferences.xml的一部分,因为它仅限于偏好
  • PreferenceScreen没有layout_widthlayout_height属性,因为不是布局元素

主要问题是您正在使用首选项片段替换android.R.id.content。这意味着您无法使用自定义布局元素,因此这为您提供了两个选项:

选项1 - 正常布局

使用您使用setContentView(...)设置的常规布局。此布局应包含片段的Toolbar和容器(例如FrameLayout),然后您将替换&#34;带有prefs片段的容器。可以通过调用setSupportActionBar(...)Toolbar设置为操作栏。请注意,在这种情况下,您必须使用Theme.AppCompat.Light.NoActionBar作为主题的父级。

选项2 - 替换android.R.id.content

如果您想坚持使用基本内容容器替换方法,您需要继续使用Theme.AppCompat.Light.DarkActionBar作为主题的父级,然后使用常用方法来控制工具栏:< / p>

可以在您的活动onOptionsItemSelected(...)中处理后退箭头点击,例如:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
  

请注意,只有在您没有在清单中设置父活动时,才应手动处理。有关详细信息,请查看Adding an Up Action指南。

最后一点,让我向您介绍我的support preferences v7 fix lib,它解决了您使用官方库可能遇到的许多问题。 v26-beta分支包含一个很好的自述文件(主服务器现在有点乱)但是主服务器(基于25.4.0)和 v26-beta (基于26.0.0-beta2)有样本应用程序(&#34; app&#34;文件夹),应该易于复制/关注以创建自己的设置屏幕。示例应用程序会显示选项1

答案 1 :(得分:1)

请查看此处的文档以添加工具栏:https://developer.android.com/training/appbar/setting-up.html

然后在操作栏上调用可怕名为setDisplayShowHomeEnabledsetDisplayHomeAsUpEnabled的内容,让它显示后退按钮。

您的标准Activity.setTitle(“设置”)将在工具栏中显示字符串“设置”。

希望有所帮助!