如何在alertDialog中更改单选按钮样式



我正在开发一个Android应用程序,我想更改AlertDialogs中RadioButton的颜色。我希望这些单选按钮与我的应用程序主题相匹配。

所以我想我应该寻找一个定义RadioButtons可绘制内容的Android属性,并在我的主题中进行修改。

首先我发现AlertDialog使用CheckedTextView来显示RadioButton。在布局select_dialog_singlechoice_holo.xml中,我发现了以下内容:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    ...
/>

然后我去了themes.xml,在Holo.Light主题中找到了这个:

<item name="listChoiceIndicatorSingle">@android:drawable/btn_radio_holo_light</item>

因此,在我的主题中,我将后者改为:

<style name="GreenTheme" parent="@android:style/Theme.Holo.Light" >
    <item name="android:listChoiceIndicatorSingle">@drawable/green_radio_button</item>
</style>

不幸的是什么都没发生。

我错过了什么?

我找到了一个解决方法。alertDialog似乎以某种方式覆盖了listChoiceIndicatorSingle属性。因此,我的解决方案是在创建选项时添加一个ArrayAdapter,从而覆盖alertDialog将使用的布局。

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
String[] items= getResources().getStringArray(choices);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_singlechoice, items);
builder.setSingleChoiceItems(adapter, checkedItem, listener);

最新更新