2011-02-07 71 views

回答

10

PreferenceActivity extends ListActivity,當你從XML與addPreferencesFromResource()膨脹的喜好,它把東西到標準ListViewListActivity用途。

所以基本上,你可以使用setContentView()來指定一個佈局,但是你需要一個ListView,它的編號爲"@+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

`@ + android:id/list`應該是`@android:id/list` – mwieczorek 2016-07-10 09:15:12

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

我試過,但沒有奏效。它不會改變任何東西。 – fhucho 2011-02-08 11:38:40

+0

這不適用於PreferenceActivity,但適用於其他活動... – 2014-06-13 10:40:12

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

您可以發佈login_settings_layout.xml嗎? – fhucho 2011-05-04 12:50:12

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

本的方法爲我工作得很好!下面是代碼

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

}

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中

Navals-的MacBook-PRO:ver_ui_0.2海軍$ VI RES /佈局/ 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>