开发者

Set custom title bar in PreferenceAcivity

I am using PreferenceActivity, how can I set a custom title bar? Not only text but background color, size - the whole layou开发者_如何学Got.


PreferenceActivity extends ListActivity, and when you inflate the preferences from xml with addPreferencesFromResource(), it puts the stuff into the standard ListView that ListActivity uses.

So basically, you can use setContentView() to specify a layout, but you need to have a ListView in it with the id "@+android:id/list".

So using kleini's example code:

protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.login_settings);
    setContentView(R.layout.login_settings_layout);
}

You would need a ListView in login_settings_layout.xml that looks something like:

<ListView 
    android:id="@+android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    /> 


This is the only thing that worked for me. Rest of the above stuff did not fetch desired results on my 4.3 Nexus Tablet.

I could not really recreate a proper actionbar like widget, but was able to put a large 'Settings' title on top of the PreferenceActivity, with below steps.

I got the hint from another stackoverflow answer and am just making it more elaborate.

  1. In PreferenceActivity class (remove the existing titlebar)

    public class Settings extends PreferenceActivity· {
      ...
      @Override
      public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE); // This goes first
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    
  2. In res/xml/settings.xml, I would like to draw attention to the first PreferenceCategory

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
       <PreferenceCategory
          android:layout="@layout/settings_titlebar" />
    
       <PreferenceCategory android:title="Notifications">
          <Preference .....
    
  3. In res/layout/settings_titlebar.xml

Navals-MacBook-Pro:ver_ui_0.2 Naval$ vi res/layout/settings_titlebar.xml

   <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:background="@drawable/header_background"
     android:enabled="false">

     <TextView android:src="@drawable/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@android:color/transparent"
        android:padding="4dip"
        android:text="Settings"
        android:textSize="32sp"
        />

   </RelativeLayout>


public class Preferences extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    super.onCreate(savedInstanceState);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
}
}


Ben's method worked out well for me!!. Here is the code

public class PreferenceCustomTitleActivity extends PreferenceActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preference);

    /** Customize your background, textsize, cachetint, dividers
        for your list view in the xml **/
    setContentView(R.layout.layout_with_simple_listview_only);

    ListView list = (ListView) findViewById(android.R.id.list);

    View preferenceHeader = getLayoutInflater().inflate(
            R.layout.preference_header, null);
    list.addHeaderView(preferenceHeader);
}

}


Awesome, worked fine for "NO_TITLE":

protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.login_settings);
    setContentView(R.layout.login_settings_layout);
}


Or like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    super.onCreate(savedInstanceState);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitlebar); 
    addPreferencesFromResource(R.xml.preferences);
}

With a customtitlebar.xml like this:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/customTitleBar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    style="@style/CustomWindowTitle">
</TextView>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜