[英]How to customize list preference radio button
I have customized all the radioButtons in my application but the radioButtons in the listPreference does not get customized.我已经自定义了我的应用程序中的所有单选按钮,但是 listPreference 中的单选按钮没有得到自定义。
I have used this xml named btn_radio.xml我使用了这个名为 btn_radio.xml 的 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>
This is the customRadioButton which extends the android custom radio button这是扩展android自定义单选按钮的customRadioButton
<style name="CustomRadioButton" Parent="@android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">@drawable/btn_radio</item>
</style>
in the theme of my application I have done this changes在我的应用程序的主题中,我已经完成了这些更改
<item name="android:radioButtonStyle">@style/CustomRadioButton</item>
<item name="android:listChoiceIndicatorSingle">@style/CustomRadioButton</item>
This changes customize all the radioButtons in my application except radioButtons in my ListPreference此更改自定义我的应用程序中的所有单选按钮,但 ListPreference 中的单选按钮除外
Styling the ListPreference
from XML is not directly possible.不能直接从 XML 样式化
ListPreference
。 The problem is that ListPreference
(through DialogPreference
) calls AlertDialog.Builder(Context)
to build its Dialog
, rather than AlertDialog.Builder(Context context, int themeResourceId)
.问题是
ListPreference
(通过DialogPreference
)调用AlertDialog.Builder(Context)
来构建它的Dialog
,而不是AlertDialog.Builder(Context context, int themeResourceId)
。 While the latter allows for providing a theme, the former does not, causing it to fall back to a default Android theme.虽然后者允许提供主题,但前者不允许,导致它回退到默认的 Android 主题。
For a project, I needed a ListPreference
with a custom title-color, a custom radiobutton-style and a custom ListView-selector (basically, Holo in a different color).对于一个项目,我需要一个带有自定义标题颜色、自定义
ListPreference
按钮样式和自定义 ListView 选择器(基本上是不同颜色的 Holo)的ListPreference
。 I solved this by extending ListPreference
and overriding onPrepareDialogBuilder(Builder)
and OnCreateDialogView()
so I could use a custom ListView with a simple ArrayAdapter, rather than Dialog
's built-in ListView
(which doesn't have support for styling).我通过扩展
ListPreference
并覆盖onPrepareDialogBuilder(Builder)
和OnCreateDialogView()
解决了这个问题,这样我就可以使用带有简单 ArrayAdapter 的自定义 ListView,而不是Dialog
的内置ListView
(它不支持样式)。 I also had to override onDialogClosed()
in order to set the right value to the preference.我还必须覆盖
onDialogClosed()
以便onDialogClosed()
选项设置正确的值。
In order to use it, all you have to do is replace the classname of the preference in your preferences.xml rom ListPreference
to com.your.packagename.ThemedListPreference
.为了使用它,您所要做的就是将您的首选项中的首选项的类名从
ListPreference
为com.your.packagename.ThemedListPreference
。 Other than that, the implementation is identical to ListPreference.除此之外,实现与 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);
}
}
}
}
For my ListView items I used the layout below.对于我的 ListView 项目,我使用了下面的布局。 Note that drawable/btn_radio_holo_light is an XML-drawable like the one in your android-sdk/platforms/android-x/data/res/drawable folder, only with references to different drawables.
请注意,drawable/btn_radio_holo_light 是一个 XML 可绘制对象,就像 android-sdk/platforms/android-x/data/res/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" />
For my Dialog layout ( onCreateDialogView()
), I used the following:对于我的对话框布局(
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.