[转]Android中自定义checkbox样式
其实很简单,分三步:
1.在drawable中创建文件checkbox_selector.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_ok" /><!--设置选中图片-->
<item android:state_checked="false"
android:drawable="@drawable/checkbox_empty" /><!--设置未选中图片-->
</selector>
2.
在values中创建styles.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox"> <item name="android:button">@drawable/checkbox_selector</item> <item name="android:paddingLeft">25.0dip</item> <item name="android:maxHeight">10.0dip</item> </style> </resources>
3.
在你的CheckBox中添加属性:
<CheckBox
android:id="@+id/check"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
style="@style/MyCheckBox"
/>
搞定!这样就把你的checkbox换成你设置的那两张图片了
1.首先在drawable文件夹中添加drawable文件checkbox_style.xml。
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:drawable="@drawable/checkbox_pressed" android:state_checked="true"/>
- <item android:drawable="@drawable/checkbox_normal" android:state_checked="false"/>
- <item android:drawable="@drawable/checkbox_normal"/>
- </selector>
2.在values文件夹下的styles.xml文件中添加CustomCheckboxTheme样式。
- <style name="CustomCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
- <item name="android:button">@drawable/checkbox_style</item>
- </style>
3.在布局文件中使用CustomCheckboxTheme样式。
- <CheckBox
- android:id="@+id/select_all"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- style="@style/CustomCheckboxTheme" />
使用到的图片资源
checkbox_normal.png
checkbox_pressed.png
在使用Android的Preference,有时为了让我们的界面更加美观,我们会自定义自己的Preference。今天就主要说一下怎样自定义CheckBoxPreference的CheckBox按钮。
系统默认CheckBoxPreference的CheckBox样式

自定义后的CheckBox样式

其实,关键的一步就是指定CheckBoxPreference的android:widgetLayout属性,详细步骤就不说了,下面直接上代码,很简单的。
1./res/xml/my_preference.xml
- <?xml version="1.0" encoding="utf-8"?>
- <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
- <CheckBoxPreference
- android:key="cbp"
- android:summaryOff="Off"
- android:summaryOn="On"
- android:title="CheckBoxPreference"
- android:widgetLayout="@layout/my_checkbox" />
- </PreferenceScreen>
2./res/layout/my_checkbox.xml
- <?xml version="1.0" encoding="utf-8"?>
- <CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+android:id/checkbox"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:button="@drawable/checkbox_checked_style"
- android:clickable="false"
- android:focusable="false" />
3./res/drawable/checkbox_checked_style.xml
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:drawable="@drawable/icon_checkbox_unchecked" android:state_checked="false"/>
- <item android:drawable="@drawable/icon_checkbox_checked" android:state_checked="true"/>
- </selector>
4.MainActivity.java注意要继承PreferenceActivity
- public class MainActivity extends PreferenceActivity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- addPreferencesFromResource(R.xml.my_preference); //这个方法已过时!
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- }
[转]Android中自定义checkbox样式的更多相关文章
- Android中自定义checkbox样式
1.首先在drawable文件夹中添加drawable文件checkbox_style.xml.
- Android之自定义checkbox样式
大部分情况下,我们在UI中并不采用Android自带的checkbox复选框样式,这时候就需要我们自定义自己的checkbox. 首先找两张checkbox背景图片,比如下图样子的: 然后在drawa ...
- Android中自定义Checkbox
custom_checkbox.xml文件: <?xml version="1.0" encoding="utf-8"?> <selector ...
- android中自定义checkbox的图片和大小
其实很简单,分三步: 1.在drawable中创建文件checkbox_selector.xml: <?xml version="1.0" encoding="ut ...
- Android 自定义CheckBox样式
1.首先在drawable文件夹中添加drawable文件checkbox_style.xml. <selector xmlns:android="http://schemas.and ...
- Android中自定义ActionBar的背景色等样式style
Android中想要去自定义ActionBar的背景色等样式. [折腾过程] 1.自己找代码,发现对应的配置的地方了: AndroidManifest.xml ? 1 2 <applicatio ...
- 自定义 checkbox 样式
前言:最近在做一个网站,为了统一风格,需要自定义checkbox的样式.所以花了点时间参考了 研究了一下.感觉上面的方法略微麻烦.所以自己重新写了下面的代码,欢迎大家指教.同时,感谢w3cplus提供 ...
- WPF 自定义CheckBox样式
自定义CheckBox样式,mark一下,方便以后参考复用 设计介绍: 1.一般CheckBox模板太难看了,肯定要重写其中的模板 2.模板状态为未选中状态和选中状态,设置为默认未选中就好了. 默认状 ...
- 转--Android中自定义字体的实现方法
1.Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace 2.在Android中可以引入其他字体 . 复制代码 代码如下: <?xml versio ...
随机推荐
- 64_p7
python-flask-whooshalchemy-0.6-10.fc26.noarch.rpm 12-Feb-2017 11:04 51894 python-flask-wtf-0.10.0-8. ...
- julia 1.0如何使用pkg
输入]进入pkg模式 add 加包名即可安装,如 add Cxx
- python代码这样写会更优雅
1.链式比较操作 age = 18 if age > 18 and age < 60: print("young man") pythonic if 18 < a ...
- css给奇数行或偶数行添加指定样式
odd表示奇数行,even表示偶数行; tr:nth-child(odd); .table-striped > tbody > tr:nth-child(odd) { background ...
- HTTPS握手过程
HTTPS在HTTP的基础上加入了SSL协议,SSL依靠证书来验证服务器的身份,并为浏览器和服务器之间的通信加密.具体是如何进行加密,解密,验证的,且看下图,下面的称为一次握手. 1. 客户端发起HT ...
- CGI、FastCGI和php-fpm的概念和区别
CGI是HTTP Server和一个独立的进程之间的协议,把HTTP Request的Header设置成进程的环境变量,HTTP Request的正文设置成进程的标准输入,而进程的标准输出就是HTTP ...
- 让Linux应用更加得心应手的
1.计算文件数和目录数 下面的语句可以帮你计算有多少个文件和多少个目录 # ls -l * |grep "^-"|wc -l ---- to count files # ls - ...
- css绘制图标
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- Perl 连接Oracle 出现OCI missing的问题及解决
问题描述 新申请了一个虚拟机操作系统: Win Server 2008, 64位 , 8核, 16G Memory 上 http://www.activestate.com/activeperl 下载 ...
- [Ext JS 4]后台自动产生图档
前言 [Ext JS 4] 实战之将chart导出为png, jpg 格式的文件 承接上一篇, 我们可以做到在Browser端打开一个Chart,并导出为png或是jpg 等格式的图档. 但实际的需求 ...