Piy*_*ush 205 android android-checkbox
如何更改CheckBox
Android中的默认颜色?
默认情况下CheckBox
颜色为绿色,我想更改此颜色.
如果不可能请告诉我如何定制CheckBox
?
afa*_*man 377
您可以直接在XML中更改颜色.使用buttonTint
的盒:(如API等级23)
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/CHECK_COLOR" />
Run Code Online (Sandbox Code Playgroud)
您也可以使用appCompatCheckbox v7
较旧的API级别执行此操作:
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/COLOR_HERE" />
Run Code Online (Sandbox Code Playgroud)
Amr*_*war 118
你可以设置复选框的android主题,以获得你在styles.xml中添加的颜色:
<style name="checkBoxStyle" parent="Base.Theme.AppCompat">
<item name="colorAccent">CHECKEDHIGHLIGHTCOLOR</item>
<item name="android:textColorSecondary">UNCHECKEDCOLOR</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后在你的布局文件中:
<CheckBox
android:theme="@style/checkBoxStyle"
android:id="@+id/chooseItemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)
不像使用android:buttonTint="@color/CHECK_COLOR"
这种方法在Api 23下工作
Mic*_*ael 114
您可以minSdkVersion
使用android:buttonTint
属性更改s drawable .
mbo*_*nin 45
程序化版本:
int states[][] = {{android.R.attr.state_checked}, {}};
int colors[] = {color_for_state_checked, color_for_state_normal}
CompoundButtonCompat.setButtonTintList(checkbox, new ColorStateList(states, colors));
Run Code Online (Sandbox Code Playgroud)
D. *_*eev 29
使用buttonTint和颜色选择器
<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/checkbox_filter_tint"
tools:targetApi="21"/>
Run Code Online (Sandbox Code Playgroud)
RES /颜色/ checkbox_filter_tint.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/light_gray_checkbox"
android:state_checked="false"/>
<item android:color="@color/common_red"
android:state_checked="true"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
Shw*_*han 16
<CheckBox
android:id="@+id/chk_remember_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@android:color/white"
android:text="@string/hint_chk_remember_me" />
Run Code Online (Sandbox Code Playgroud)
小智 16
我建议在android中使用他的样式方法来配置内置的android视图,在你的项目中添加新的样式:
<style name="yourStyle" parent="Base.Theme.AppCompat">
<item name="colorAccent">your_color</item> <!-- for uncheck state -->
<item name="android:textColorSecondary">your color</item> <!-- for check state -->
</style>
Run Code Online (Sandbox Code Playgroud)
并将此样式添加到复选框的主题:android:theme ="@ style/youStyle"
希望这可以帮助.
Yog*_*thi 11
在您的styles.xml
文件中添加以下行:
<style>
<item name="android:colorAccent">@android:color/holo_green_dark</item>
</style>
Run Code Online (Sandbox Code Playgroud)
buttonTint为我工作尝试
机器人:buttonTint = "@色/白色"
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/agreeCheckBox"
android:text="@string/i_agree_to_terms_s"
android:buttonTint="@color/white"
android:layout_below="@+id/avoid_spam_text"/>
Run Code Online (Sandbox Code Playgroud)
小智 8
我遇到了同样的问题,我用下面的技术得到了一个解决方案.将btn_check.xml
from 复制android-sdk/platforms/android-#(version)/data/res/drawable
到项目的drawable文件夹,并将"on"和"off"图像状态更改为自定义图像.
然后你的xml将需要android:button="@drawable/btn_check"
<CheckBox
android:button="@drawable/btn_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
Run Code Online (Sandbox Code Playgroud)
如果您想使用不同的默认Android图标,您可以使用:
android:button="@android:drawable/..."
Run Code Online (Sandbox Code Playgroud)
创建一个XMLDrawable resource file
下,res->drawable
并将其命名,例如,checkbox_custom_01.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="@drawable/checkbox_custom_01_checked_white_green_32" />
<item
android:state_checked="false"
android:drawable="@drawable/checkbox_custom_01_unchecked_gray_32" />
</selector>
Run Code Online (Sandbox Code Playgroud)
将您的自定义复选框图像文件(我推荐 png)上传到您的res->drawable
文件夹。
然后进入您的布局文件并将您的复选框更改为
<CheckBox
android:id="@+id/checkBox1"
android:button="@drawable/checkbox_custom_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="CheckBox"
android:textSize="32dip"/>
Run Code Online (Sandbox Code Playgroud)
您可以自定义任何内容,只要android:button
指向您之前创建的正确 XML 文件即可。
新手注意:虽然这不是强制性的,但在整个布局树中使用唯一 id 命名复选框仍然是一个好习惯。
100% 稳健的方法。
就我而言,我无法访问 XML 布局源文件,因为我从第 3 方 MaterialDialog 库获取了 Checkbox。所以我必须以编程方式解决这个问题。
res/color/ checkbox_tinit_dark_theme.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white"
android:state_checked="false"/>
<item android:color="@color/positiveButtonBg"
android:state_checked="true"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
然后将其应用到复选框:
ColorStateList darkStateList = ContextCompat.getColorStateList(getContext(), R.color.checkbox_tint_dark_theme);
CompoundButtonCompat.setButtonTintList(checkbox, darkStateList);
Run Code Online (Sandbox Code Playgroud)PS 此外,如果有人感兴趣,以下是如何从 MaterialDialog 对话框中获取复选框的方法(如果您使用 进行设置.checkBoxPromptRes(...)
):
CheckBox checkbox = (CheckBox) dialog.getView().findViewById(R.id.md_promptCheckbox);
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
如果 textColorSecondary 不适合您,您可能已在主题中将 colorControlNormal 定义为不同的颜色。如果是这样,只需使用
<style name="yourStyle" parent="Base.Theme.AppCompat">
<item name="colorAccent">your_checked_color</item> <!-- for checked state -->
<item name="colorControlNormal">your_unchecked_color</item> <!-- for unchecked state -->
</style>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
215572 次 |
最近记录: |