Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中为复选框勾选设置白色背景?_Java_Android - Fatal编程技术网

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

Java 如何在Android中为复选框勾选设置白色背景?,java,android,Java,Android,我正在为4.0版本开发Android应用程序,所有复选框都有黑色背景,但我需要白色背景。如果我使用“background”xml属性作为“white”,那么所有复选框(带文本)都是白色的;我只需要一个白色的地方 最简单的方法-使用具有3种背景状态的stateSelected=true,stateSelected=false,statespressed=true)模拟复选框只需创建具有3个背景的适当xml文件,并将其设置为ImageViewbackground然后在代码中,单击ImageView只

我正在为4.0版本开发Android应用程序,所有复选框都有黑色背景,但我需要白色背景。如果我使用“background”xml属性作为“white”,那么所有复选框(带文本)都是白色的;我只需要一个白色的地方

最简单的方法-使用具有3种背景状态的
stateSelected=true
stateSelected=false
statespressed=true
)模拟
复选框

只需创建具有3个背景的适当xml文件,并将其设置为
ImageView
background

然后在代码中,单击
ImageView
只需切换
ImageView.setSelected=!ImageView.isSelected


希望它能帮上忙

我不知道我是不是迟到太晚了还是怎么了,但下面是答案

代码片段:

将此选择器命名为“复选框\选择器”


为了只针对复选框而不是整个文本进行设置,您可以将其设置为:

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

也许我的解决方案不是很优雅,但它适合我:

我简单地创建了另一个具有复选框矩形(15x15)尺寸和白色背景的布局。然后,我将这个布局添加到RelativeLayout中,并在其中放置复选框。边距为9dp,我将白色布局矩形放在复选框下方,因此复选框的底色似乎是白色的

        <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>

        <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>