2011-02-07 29 views

Trả lời

10

PreferenceActivity extends ListActivity, và khi bạn thổi phồng các ưu đãi từ xml với addPreferencesFromResource(), nó đặt những thứ vào tiêu chuẩn ListView rằng ListActivity sử dụng.

Vì vậy, về cơ bản, bạn có thể sử dụng setContentView() để chỉ định bố cục, nhưng bạn cần có một ListView trong đó với id "@+android:id/list".

Vì vậy, sử dụng mã ví dụ kleini của:

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

Bạn sẽ cần một ListView trong login_settings_layout.xml trông giống như sau:

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

'@ + android: id/danh sách' phải là' @android: id/list' – mwieczorek

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

tôi đã cố gắng đó, nhưng nó đã không làm việc. Nó không thay đổi gì cả. – fhucho

+0

Tính năng này không hoạt động đối với PreferenceActivity nhưng hoạt động tốt cho các Hoạt động khác ... –

0

Awesome, làm việc tốt cho "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

Bạn có thể đăng login_settings_layout.xml không? – fhucho

0

Hoặc như thế này:

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

Với customtitlebar .xml như thế này:

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

Phương pháp của Ben hoạt động tốt cho tôi !!. Đây là mã

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

}

+0

cách thay đổi văn bản? –

1

Đây là điều duy nhất mà làm việc cho tôi. Phần còn lại của các công cụ trên không tìm nạp kết quả mong muốn trên máy tính bảng 4,3 Nexus của tôi.

Tôi thực sự không thể tạo lại một thanh tác vụ thích hợp như tiện ích, nhưng có thể đặt tiêu đề 'Cài đặt' lớn ở đầu PreferenceActivity, với các bước dưới đây.

Tôi nhận được gợi ý từ một câu trả lời xếp chồng khác và chỉ làm cho nó phức tạp hơn.

  1. Trong lớp PreferenceActivity (loại bỏ các thanh tiêu đề hiện có)

    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. Trong res/xml/settings.xml, tôi muốn gây sự chú ý đến PreferenceCategory đầu tiên

    <?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. Trong 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> 
Các vấn đề liên quan