Android

时间:2017-07-11 17:39:06

标签: android

我是Android的新手,我在显示PreferenceScreen活动的标题时遇到问题。我想要显示这样的标题

enter image description here

我的班级声明就像这样

public class SettingsActivity extends PreferenceActivity

onCreate喜欢这个

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    _settingsFragment = new SettingsFragment();

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

的preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <Preference
        android:key="pref_key_vault_storage"
        android:title="Vault Storage"
        android:summary="Set/modify the vault storage location." >
        <intent
            android:targetPackage="com.test.concealx"
            android:targetClass="com.test.concealx.ChooseStorageActivity"/>
    </Preference>
    <EditTextPreference
        android:id="@+id/id_path"
        android:title="Vault Path"
        android:key="pref_key_vault_path"
        android:summary="Set/modify the storage path. DO NOT change this unless you are sure about it.">
    </EditTextPreference>
</PreferenceScreen>

我现在得到的是这个

enter image description here

我已经在PreferenceActivity上尝试android:title="My Settings"setTitle无济于事。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

将PreferenceActivity更改为AppCompatPretenceActivity

public class SettingsActivity extends PreferenceActivity

你需要像这样设置ActionBar,因为它上面会显示标题:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    _settingsFragment = new SettingsFragment();

    getFragmentManager().beginTransaction().replace(android.R.id.content, _settingsFragment).commit(); 


     Toolbar toolbar;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            ViewGroup root = (ViewGroup) findViewById(android.R.id.list).getParent().getParent().getParent();
            toolbar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
            root.addView(toolbar, 0);
        } else {
            ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
            ListView content = (ListView) root.getChildAt(0);
            root.removeAllViews();
            toolbar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
            int height;
            TypedValue tv = new TypedValue();
            if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
                height = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
            } else {
                height = toolbar.getHeight();
            }
            content.setPadding(0, height, 0, 0);
            root.addView(content);
            root.addView(toolbar);
        }
        setSupportActionBar(toolbar);

}

settings_toolbar.xml:

    <?xml version="1.0" encoding="utf-8"?>
<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/toolbar2"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.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"
    />