2012-05-05 43 views
11

Tôi đã tùy chỉnh tất cả các nút radio trong ứng dụng của mình nhưng radioButtons trong listPreference không được tùy chỉnh.Cách tùy chỉnh nút radio tùy chọn danh sách

Tôi đã sử dụng xml này tên btn_radio.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_checked="true" android:state_window_focused="false" 
     android:drawable="@drawable/radio_selected" /> 
<item android:state_checked="false" android:state_window_focused="false" 
     android:drawable="@drawable/radio_unselected" /> 

<item android:state_checked="true" android:state_pressed="true" 
     android:drawable="@drawable/radio_selected" /> 
<item android:state_checked="false" android:state_pressed="true" 
     android:drawable="@drawable/radio_unselected" /> 

<item android:state_checked="true" android:state_focused="true" 
     android:drawable="@drawable/radio_selected" /> 
<item android:state_checked="false" android:state_focused="true" 
     android:drawable="@drawable/radio_unselected" /> 

<item android:state_checked="false" android:drawable="@drawable/radio_unselected" /> 
<item android:state_checked="true" android:drawable="@drawable/radio_selected" /> 
</selector> 

Đây là customRadioButton mà mở rộng nút radio android tùy chỉnh

<style name="CustomRadioButton" Parent="@android:style/Widget.CompoundButton.RadioButton"> 
    <item name="android:button">@drawable/btn_radio</item> 
</style> 

trong chủ đề của ứng dụng của tôi, tôi đã làm điều này thay đổi

<item name="android:radioButtonStyle">@style/CustomRadioButton</item> 
    <item name="android:listChoiceIndicatorSingle">@style/CustomRadioButton</item> 

Thay đổi này customiz e tất cả các nút radio trong ứng dụng của tôi ngoại trừ radioButtons trong ListPreference của tôi

+0

plz tôi đang chờ đợi –

Trả lời

24

Tạo kiểu ListPreference từ XML không trực tiếp thực hiện được. Vấn đề là ListPreference (thông qua DialogPreference) gọi số AlertDialog.Builder(Context) để tạo số Dialog, thay vì AlertDialog.Builder(Context context, int themeResourceId). Trong khi sau này cho phép cung cấp một chủ đề, trước đây không, làm cho nó rơi trở lại một chủ đề Android mặc định. Đối với một dự án, tôi cần một ListPreference với màu tiêu đề tùy chỉnh, kiểu tùy chỉnh kiểu vô tuyến và bộ chọn ListView tùy chỉnh (về cơ bản, Holo có màu khác). Tôi đã giải quyết vấn đề này bằng cách mở rộng ListPreference và ghi đè onPrepareDialogBuilder(Builder)OnCreateDialogView() để tôi có thể sử dụng ListView tùy chỉnh với ArrayAdapter đơn giản, thay vì Dialog được xây dựng trong ListView (không hỗ trợ tạo kiểu). Tôi cũng phải ghi đè onDialogClosed() để đặt giá trị phù hợp cho tùy chọn.

Để sử dụng, tất cả những gì bạn phải làm là thay thế tên lớp của tùy chọn trong rom preferences.xml của bạn ListPreference thành com.your.packagename.ThemedListPreference. Ngoài ra, việc triển khai thực hiện giống với ListPreference.

public class ThemedListPreference extends ListPreference implements OnItemClickListener { 

    public static final String TAG = "ThemedListPreference"; 

    private int mClickedDialogEntryIndex; 

    private CharSequence mDialogTitle; 

    public ThemedListPreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public ThemedListPreference(Context context) { 
     super(context); 
    } 

    @Override 
    protected View onCreateDialogView() { 
     // inflate custom layout with custom title & listview 
     View view = View.inflate(getContext(), R.layout.dialog_settings_updatetime, null); 

     mDialogTitle = getDialogTitle(); 
     if(mDialogTitle == null) mDialogTitle = getTitle(); 
     ((TextView) view.findViewById(R.id.dialog_title)).setText(mDialogTitle); 

     ListView list = (ListView) view.findViewById(android.R.id.list); 
     // note the layout we're providing for the ListView entries 
     ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
       getContext(), R.layout.btn_radio, 
       getEntries()); 

     list.setAdapter(adapter); 
     list.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     list.setItemChecked(findIndexOfValue(getValue()), true); 
     list.setOnItemClickListener(this); 

     return view; 
    } 

    @Override 
    protected void onPrepareDialogBuilder(Builder builder) { 
     // adapted from ListPreference 
     if (getEntries() == null || getEntryValues() == null) { 
      // throws exception 
      super.onPrepareDialogBuilder(builder); 
      return; 
     } 

     mClickedDialogEntryIndex = findIndexOfValue(getValue()); 

     // .setTitle(null) to prevent default (blue) 
     // title+divider from showing up 
     builder.setTitle(null); 

     builder.setPositiveButton(null, null); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, 
      long id) { 
     mClickedDialogEntryIndex = position; 
     ThemedListPreference.this.onClick(getDialog(), DialogInterface.BUTTON_POSITIVE); 
     getDialog().dismiss(); 
    } 

    @Override 
    protected void onDialogClosed(boolean positiveResult) { 
      // adapted from ListPreference 
     super.onDialogClosed(positiveResult); 

     if (positiveResult && mClickedDialogEntryIndex >= 0 
       && getEntryValues() != null) { 
      String value = getEntryValues()[mClickedDialogEntryIndex] 
        .toString(); 
      if (callChangeListener(value)) { 
       setValue(value); 
      } 
     } 
    } 
} 

Đối với các mục ListView tôi đã sử dụng bố cục bên dưới. Lưu ý rằng drawable/btn_radio_holo_light là một XML có thể vẽ như một trong thư mục android-sdk/platforms/android-x/data/res/drawable của bạn, chỉ với các tham chiếu đến các đối tượng vẽ khác nhau.

<?xml version="1.0" encoding="utf-8"?> 
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:checkMark="@drawable/btn_radio_holo_light" 
    android:gravity="center_vertical" 
    android:minHeight="@dimen/list_item_minheight" 
    android:paddingLeft="@dimen/list_item_paddingLeft" 
    android:paddingRight="@dimen/list_item_paddingLeft" /> 

Đối với bố trí Dialog của tôi (onCreateDialogView()), tôi sử dụng như sau: Câu trả lời

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/dialog_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:textColor="@color/title_color" 
     android:textSize="22sp" /> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="2dp" 
     android:background="@color/divider_color" /> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:listSelector="@drawable/list_selector" /> 

</LinearLayout> 
+0

gì là 'CheckedTextView' ở đây, là nó bất kỳ giao diện tùy chỉnh được tạo ra. Bạn có thể vui lòng hướng dẫn. – Dory

+0

nên có gì trong bố cục 'R.layout.btn_radio' này. – Dory

+0

@NidhiGondhia, [CheckedTextView] (http://developer.android.com/reference/android/widget/CheckedTextView.html) là một tiện ích Android chuẩn. Đoạn mã bắt đầu bằng ' Felix

Các vấn đề liên quan