繁体   English   中英

对话框中的自定义复选框样

[英]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=14targetSdkVersion=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源代码发现了这一点,并发现对话框中用于多项选项的模板是这样的:

https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/select_dialog_multichoice_holo.xml

您可以在文档中的主题和样式中找到您要查找的所有内容,并将主题的属性应用于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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM