在PreferenceActivity中设置自定义标题栏

时间:2011-02-07 13:26:47

标签: android

我正在使用PreferenceActivity,如何设置自定义标题栏?不仅是文字,还有背景颜色,大小 - 整个布局。

6 个答案:

答案 0 :(得分:11)

PreferenceActivity extends ListActivity,当您使用addPreferencesFromResource()从xml中提升偏好设置时,它会将内容放入ListView使用的标准ListActivity中。

基本上,您可以使用setContentView()来指定布局,但您需要在ListView中添加ID "@+android:id/list"

所以使用kleini的示例代码:

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

你需要login_settings_layout.xml中的ListView看起来像:

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

答案 1 :(得分:1)

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);
}
}

答案 2 :(得分:1)

Ben的方法很适合我!!这是代码

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);
}

}

答案 3 :(得分:1)

这是唯一对我有用的东西。上述其他内容无法在我的4.3 Nexus平板电脑上获取所需的结果。

我无法真正重新创建一个像widget这样的正确动作栏,但是能够在PreferenceActivity上放置一个大的“设置”标题,步骤如下。

我从另一个stackoverflow回答得到了提示,我只是更精细。

  1. 在PreferenceActivity类中(删除现有标题栏)

    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. 在res / xml / settings.xml中,我想提请注意第一个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. 在res / layout / settings_titlebar.xml

  4. 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>
    

答案 4 :(得分:0)

太棒了,为“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);
}

答案 5 :(得分:0)

或者像这样:

@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);
}

使用这样的customtitlebar.xml:

<?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>