2012-05-05 122 views
11

我已经定制了我的应用程序中的所有radioButton,但listPreference中的radioButtons没有得到定制。如何自定义列表首选项单选按钮

我已经使用这个XML命名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> 

这是延伸在我的应用程序的主题Android的定制单选按钮

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

的customRadioButton我已经这样做了改变

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

这改变了customiz e除我的应用程序中的radioButton之外的所有radioButtons

+0

PLZ答案我在等 –

回答

24

从XML中设计ListPreference不是直接可能的。问题是ListPreference(通过DialogPreference)调用AlertDialog.Builder(Context)来构建其Dialog,而不是AlertDialog.Builder(Context context, int themeResourceId)。虽然后者允许提供一个主题,但前者不会导致它回退到默认的Android主题。

对于一个项目,我需要一个ListPreference具有自定义标题颜色,自定义单选按钮,风格和自定义的ListView选择器(基本上,河洛不同的颜色)。我通过扩展ListPreference和覆盖onPrepareDialogBuilder(Builder)OnCreateDialogView()解决了这个问题,所以我可以使用一个简单的ArrayAdapter自定义ListView,而不是Dialog的内置ListView(它不支持样式)。我还必须覆盖onDialogClosed()以便为偏好设置正确的值。

为了使用它,您所要做的就是将preferences.xml rom ListPreference中的首选项的类名替换为com.your.packagename.ThemedListPreference。除此之外,该实现与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); 
      } 
     } 
    } 
} 

对于我的ListView项目,我使用了下面的布局。请注意,drawable/btn_radio_holo_light是一个类似于android-sdk/platforms/android-x/data/res/drawable文件夹中的drawable/btn_radio_holo_light,只能引用不同的drawable。

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

对于我的对话框布局(onCreateDialogView()),我用了以下内容:

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

是什么'CheckedTextView'这里,是它创建的任何自定义视图。你能请指导。 – Dory

+0

这个'R.layout.btn_radio'布局应该是什么。 – Dory

+0

@NidhiGondhia,[CheckedTextView](http://developer.android.com/reference/android/widget/CheckedTextView.html)是一个标准的Android小部件。以' Felix