Android 自定义CheckBox 样式

摘要:
state_checked=“false”android:state_checked=“true”android:xmlversion=“1.0”encoding=“UTF-8”;&lt?//schemas.android.com/apk/res/android“&gt:&lt:state_pressed=“true”android:state_checked=“false”android;

新建Android XML文件,类型选Drawable,根结点选selector,在这定义具体的样式。

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:state_pressed="true"
                android:drawable="@drawable/focused" ;/>
        <item android:state_checked="false" android:state_pressed="true"
                android:drawable="@drawable/normal" ;/>
        <item android:state_checked="false" android:drawable="@drawable/normal" ;/>
        <item android:state_checked="true" android:drawable="@drawable/focused" ;/>
</selector>

state_checked 选中状态 state_pressed按下状态 
即分别设置checkbox选中和没选中时,按下和没按下时显示的图片. 
应用到Checkbox与Button不同,并不是设置Background属性,而是设置style属性,所以我们要写一个style。 
在strings.xml写一个style,

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:state_pressed="true"
                android:drawable="@drawable/focused" ;/>
        <item android:state_checked="false" android:state_pressed="true"
                android:drawable="@drawable/normal" ;/>
        <item android:state_checked="false" android:drawable="@drawable/normal" ;/>
        <item android:state_checked="true" android:drawable="@drawable/focused" ;/>
</selector>

应用到Checkbox:

<CheckBox android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        style="@style/MyCheckBox"
;        />

免责声明:文章转载自《Android 自定义CheckBox 样式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇维护Panel的滚动条ScrollBars位置(C#)MATLAB中矢量场图的绘制 (quiver/quiver3/dfield/pplane) Plot the vector field with MATLAB下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Mybatis-第N篇配置log4j1、log4j2打印执行的sql语句

1、log4j1配置    目录结构:       conf.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/myba...

Android实现简单的检测手机自由落体关闭屏幕

实现功能如下:在背景运行app,检测到自由落体状态时,熄灭屏幕,可重复测试。 1. 检测自由落体动作  需要使用到加速度感应器 TYPE_ACCELEROMETER SensorManager mSensorManager; private float mLastX; private float mLastY; private float mLastZ;...

Python 开发安卓Android及IOS应用库Kivy安装尝试

Python 开发安卓Android及IOS应用库Kivy安装尝试: 先来看看这货可以用来制作什么应用: Create a package for Windows Create a package for Android Creating packages for OS X Create a package for IOS 逆天的节奏啊,虽然600+页...

HTML往div中赋值

HTML中往div赋值 <div id="mazey">content</div>   使用JavaScript: var mazey=document.getElementById("mazey"); mazey.innerHTML=data;  使用jQuery: $("#mazey").html(data);  ...

Android开发之Instrumentation(自动化测试)

Android在JUnit的之外给我们又提供了Instrumentation测试框架。通过Instrumentation可以模拟按键按下、抬起、屏幕点击、滚动等事件,有效地控制Activity进行自动化测试。 Instrumentation是执行application instrumentation代码的基类。当应用程序运行的时候instrumentati...

SUSE12SP3-Mysql5.7安装

1、将以下安装包复制到服务器 mysql-community-client-5.7.24-1.sles12.x86_64.rpm mysql-community-server-5.7.24-1.sles12.x86_64.rpm mysql-community-libs-5.7.24-1.sles12.x86_64.rpm mysql-community-...