简体   繁体   English

如何在Android中为复选框打勾设置白色背景?

[英]How to set white background for checkbox tick in Android?

I'm developing Android app for 4.0 version, and all checkboxes have got a black background for tick, but I need white. 我正在开发适用于4.0版本的Android应用,并且所有复选框的刻度线都有黑色背景,但是我需要白色。 If I use "background" xml attribute as "white" then all checkbox (with text) is white; 如果我将“ background” xml属性用作“ white”,则所有复选框(带有文本)都是白色; I need only white place for tick. 我只需要在白色的地方打勾。

The most simple way - emulate CheckBox using ImageView with 3 background states ( stateSelected=true , stateSelected=false , statePressed = true ). 最简单的方法-使用具有3个背景状态( stateSelected=truestateSelected=falsestatePressed = true )的ImageView模拟CheckBox
Just create appropriate xml file with 3 backgrounds and set it to ImageView background . 只需创建具有3个背景的合适的xml文件并将其设置为ImageView background
Then in code, when click on ImageView just switch ImageView.setSelected = !ImageView.isSelected . 然后在代码中,当单击ImageView只需切换ImageView.setSelected = !ImageView.isSelected

Hope its help 希望它的帮助

I dont know if i am extremely late or what, but here is the solution 我不知道我是否很晚或什么,但这是解决方案

Code snippet: 程式码片段:

say this selector is name "checkbox_selector" 说这个选择器的名字是“ checkbox_selector”

<?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/cbchk_blue"
    android:state_focused="false">
</item>
<item android:state_checked="true" 
    android:drawable="@drawable/cbchk_blue"
    android:state_focused="true">
</item>
<item android:state_checked="false" 
    android:drawable="@drawable/cbunchk_blue"
    android:state_focused="false">
</item>
<item android:state_checked="false" 
    android:drawable="@drawable/cbunchk_blue"
    android:state_focused="true">
</item>

in order to set it just for the checkbox and not the entire text also, you could have it as: 为了仅将其设置为复选框而不是整个文本,您可以将其设置为:

<CheckBox
     android:layout_width="45dip"
     android:layout_height="wrap_content"
     android:layout_centerVertical="true"
     android:button="@drawable/checkbox_selector"
     android:text="@string/text_here" />

Maybe my solution is not very elegant, but it works for me: 也许我的解决方案不是很好,但是对我有用:

I simplily create another layout with the dimensions of the checkbox rectangle (15x15) and with white background. 我用复选框矩形(15x15)的尺寸和白色背景简单地创建了另一个布局。 Then I add this layout to a RelativeLayout, where I also put the checkbox. 然后,将此布局添加到RelativeLayout中,并在其中放置复选框。 With a margin of 9dp, I put the white layout-rectangle below the checkbox, so it seems that the checkbox bacground color is white. 以9dp的边距,我将白色的layout-rectangle放置在复选框的下方,因此看来该复选框的bacground颜色是白色的。

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/login_large_margin"
            android:layout_marginTop="@dimen/login_medium_margin" >

            <RelativeLayout
                android:layout_width="15dp"
                android:layout_height="15dp"
                android:layout_margin="9dp"
                android:background="@color/white" >
            </RelativeLayout>

            <CheckBox
                android:id="@+id/login_check"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-light"
                android:text="@string/login_check"
                android:textColor="@color/white" >
            </CheckBox>
        </RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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