[英]Custom checkbox style in dialog
我正在構建一個包含多項選項(復選框)的對話框:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMultiChoiceItems(arrayResource, selectedItems, new DialogInterface.OnMultiChoiceClickListener() {
// ...
});
AlertDialog dialog = builder.create();
dialog.show();
我有一個復選框的自定義樣式:
<style name="CustomCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/btn_check</item>
<item name="android:textColor">@android:color/holo_purple</item>
</style>
通過設置style="@style/CustomCheckBox"
,它在應用於布局中的各個復選框時非常style="@style/CustomCheckBox"
。
但是,如何將此樣式應用於創建的對話框? 或者對整個主題來說......
如果它在某種程度上相關 - 我使用minSdkVersion=14
和targetSdkVersion=19
。
更新:
現在根據MattMatt的回答,我將自定義復選框樣式應用於整個主題,並為對話框設置自定義樣式:
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:checkboxStyle">@style/CustomCheckBox</item>
<item name="android:dialogTheme">@style/CustomDialog</item>
<item name="android:alertDialogTheme">@style/CustomDialog</item>
</style>
<style name="CustomCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/btn_check</item>
<item name="android:checkMark">@drawable/btn_check</item>
</style>
<style name="CustomDialog" parent="@android:style/Theme.Holo.Light.Dialog">
<item name="android:checkboxStyle">@style/CustomCheckBox</item>
<item name="android:background">#aaf</item>
</style>
現在添加到布局的任何復選框都會獲得自定義樣式,我可以更改對話框的背景,但對話框中的復選框不會受到任何影響......
實際上,調用setMultiChoiceItems不會生成CheckBox小部件,而是生成CheckedTextView小部件。 [更新]似乎更改checkMark屬性的值沒有任何效果。 但是,通過更改CustomDialog樣式的listChoiceIndicatorMultiple屬性的值,我能夠更改選項的drawable。 像這樣:
<style name="CustomDialog" parent="@android:style/Theme.Holo.Light.Dialog">
<item name="android:listChoiceIndicatorMultiple">@drawable/btn_check</item>
<item name="android:background">#aaf</item>
</style>
我通過查看Android源代碼發現了這一點,並發現對話框中用於多項選項的模板是這樣的:
您可以在文檔中的主題和樣式中找到您要查找的所有內容,並將主題的屬性應用於Android attrs docs中的屬性
為了方便您,請按照以下示例操作:
<style name="myTheme" parent="@android:Theme.Holo">
<!-- override default check box style with custom checkBox -->
<item name="android:checkBoxStyle">@style/CustomCheckBox</item>
</style>
<style name="CustomCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/btn_check</item>
<item name="android:textColor">@android:color/holo_purple</item>
現在,只要“myTheme”設置為Activity,就會有一個自定義復選框;)
希望這有幫助,快樂編碼!
您可以為所有復選框狀態設置drawable。 Drawables可能是圖片或形狀,因此您可以自由設置您想要的任何背景。
1>根據需要為cb選中和取消選中狀態制作兩個png文件,並將它們存儲在應用的可繪制文件夾中。
2>現在制作一個customdrawble.xml文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/yourdrawable1" />
<item android:state_checked="true" android:drawable="@drawable/yourdrawable2" />
<item android:drawable="@drawable/yourdrawable1" /> <!-- default -->
</selector>
3>現在將您的復選框設置如下:
<CheckBox
android:id="@+id/chk"
android:button=“@drawable/customdrawable”
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
該解決方案適用於我更改單個復選框的顯示
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.