如何在android中将ToolBar设置为PreferenceScreen

时间:2016-02-25 08:55:41

标签: android xml

我想在我的应用程序设置中使用ToollBar,但我希望在 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" app:theme="@style/ThemeOverlay.AppCompat.ActionBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" app:navigationContentDescription="@string/abc_action_bar_up_description" android:background="?attr/colorPrimary" app:navigationIcon="?attr/homeAsUpIndicator" app:title="Setting Page" /> 下方显示偏好设置
toolbar.xml代码:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:key="setting_title_title_category"
        android:title="Title options">

    <CheckBoxPreference
        android:id="@+id/setting_title_show_id"
        android:key="setting_title_show"
        android:title="Show Main Title"
        android:summary="Show/hide MainPage title"
        android:checked="true"/>

    <EditTextPreference
        android:key="setting_title_text"
        android:title="Set Main Title"
        android:summary="Change MainPage title"
        android:dialogTitle="Change Title"
        android:dialogMessage="Change title please..."/>

    </PreferenceCategory>

    <PreferenceCategory
        android:key="setting_title_font_category"
        android:title="Font options">

        <ListPreference
            android:key="setting_title_font_color"
            android:title="Title font colors"
            android:summary="Change title font colors"
            android:entries="@array/colors"
            android:entryValues="@array/colors"
            android:dialogTitle="Change font color" />

    </PreferenceCategory>

</PreferenceScreen>

setting_preferences.xml(路径为:res / xml)代码:

public class SettingPage extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals("setting_title_font_color")) {
            // get preference by key
            Preference pref = findPreference(key);
            // do your stuff here
        }

        if (key.equals("setting_title_show")){

            Preference pref = findPreference(key);
        }
    }

    public static class MyPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.setting_prefrences);
        }
    }

设置页面代码:

.images img{
  flex:1;
  max-width:100%;
  max-height:100%;
}

怎么回事? show setting_preferences.xml beloe ToolBar。

1 个答案:

答案 0 :(得分:0)

我相信您可以使用包含Activity的简单布局的普通AppCompatActivity / Toolbar。然后在Activity布局的内容区域中,使用PreferenceFragment来显示PreferenceScreen