How to remove the title bar from PreferenceCategory in PreferenceActivity

拈花ヽ惹草 提交于 2019-12-11 04:17:12

问题


This is my preferences xml file: myPreferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory>
    <EditTextPreference android:key="name" android:title="Name" android:inputType="text" android:defaultValue="" />
    <EditTextPreference android:key="email" android:title="Email" android:inputType="textEmailAddress" android:defaultValue="" />
    <EditTextPreference android:key="phone" android:title="Phone Number" android:inputType="phone" android:defaultValue="" />
    <EditTextPreference android:key="zipcode" android:title="Zip Code" android:inputType="number" android:defaultValue="" />
</PreferenceCategory>
</PreferenceScreen>

This is my preferences activity declaration in my AndroidManifest.xml:

<activity
        android:name=".activities.MyPreferencesActivity"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar" />

And, this is a style I have tried applying to my activity before:

 <activity
     android:name=".activities.MyPreferencesActivity"
     android:screenOrientation="portrait"
     android:theme="@android:style/PreferenceTheme" />

 <style name="PreferenceTheme">
     <item name="android:background">@drawable/background_preferences</item>
     <item name="android:windowNoTitle">true</item>
 </style>

It doesn't work.

Then, I've tried doing it through java code in my onCreate of myPreferencesActivity:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     requestWindowFeature(Window.FEATURE_NO_TITLE);
     super.onCreate(savedInstanceState);
 addPreferencesFromResource(R.xml.myPreferences);
 }

Doesn't work. Tried setting the title to a drawable.

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
     super.onCreate(savedInstanceState);
     addPreferencesFromResource(R.xml.preferences_profile);
     getWindow().setFeatureDrawableResource(Window.FEATURE_CUSTOM_TITLE, R.drawable.profile_banner);
 }

NOPE. Doesn't work.

Everything I did only removes the title bar of the preference activity but not the preference category.

Could someone please help me? Thanks in advance.


回答1:


If you don't want to have a title bar above a group of preferences, don't use a PreferenceCategory as it is serving no purpose, and just put all your preferences into the PreferenceScreen element:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <EditTextPreference android:key="name" android:title="Name" android:inputType="text" android:defaultValue="" />
    <EditTextPreference android:key="email" android:title="Email" android:inputType="textEmailAddress" android:defaultValue="" />
    <EditTextPreference android:key="phone" android:title="Phone Number" android:inputType="phone" android:defaultValue="" />
    <EditTextPreference android:key="zipcode" android:title="Zip Code" android:inputType="number" android:defaultValue="" />

</PreferenceScreen>

HTH



来源:https://stackoverflow.com/questions/10555914/how-to-remove-the-title-bar-from-preferencecategory-in-preferenceactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!