其实很简单,分三步:

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。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@drawable/checkbox_pressed" android:state_checked="true"/>
  4. <item android:drawable="@drawable/checkbox_normal" android:state_checked="false"/>
  5. <item android:drawable="@drawable/checkbox_normal"/>
  6. </selector>

2.在values文件夹下的styles.xml文件中添加CustomCheckboxTheme样式。

  1. <style name="CustomCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
  2. <item name="android:button">@drawable/checkbox_style</item>
  3. </style>

3.在布局文件中使用CustomCheckboxTheme样式。

  1. <CheckBox
  2. android:id="@+id/select_all"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. 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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <CheckBoxPreference
  4. android:key="cbp"
  5. android:summaryOff="Off"
  6. android:summaryOn="On"
  7. android:title="CheckBoxPreference"
  8. android:widgetLayout="@layout/my_checkbox" />
  9. </PreferenceScreen>

2./res/layout/my_checkbox.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+android:id/checkbox"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:button="@drawable/checkbox_checked_style"
  7. android:clickable="false"
  8. android:focusable="false" />

3./res/drawable/checkbox_checked_style.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@drawable/icon_checkbox_unchecked" android:state_checked="false"/>
  4. <item android:drawable="@drawable/icon_checkbox_checked" android:state_checked="true"/>
  5. </selector>

4.MainActivity.java注意要继承PreferenceActivity

    1. public class MainActivity extends PreferenceActivity {
    2. @Override
    3. public void onCreate(Bundle savedInstanceState) {
    4. super.onCreate(savedInstanceState);
    5. addPreferencesFromResource(R.xml.my_preference);  //这个方法已过时!
    6. }
    7. @Override
    8. public boolean onCreateOptionsMenu(Menu menu) {
    9. getMenuInflater().inflate(R.menu.activity_main, menu);
    10. return true;
    11. }
    12. }

[转]Android中自定义checkbox样式的更多相关文章

  1. Android中自定义checkbox样式

    1.首先在drawable文件夹中添加drawable文件checkbox_style.xml.

  2. Android之自定义checkbox样式

    大部分情况下,我们在UI中并不采用Android自带的checkbox复选框样式,这时候就需要我们自定义自己的checkbox. 首先找两张checkbox背景图片,比如下图样子的: 然后在drawa ...

  3. Android中自定义Checkbox

    custom_checkbox.xml文件: <?xml version="1.0" encoding="utf-8"?> <selector ...

  4. android中自定义checkbox的图片和大小

    其实很简单,分三步: 1.在drawable中创建文件checkbox_selector.xml: <?xml version="1.0" encoding="ut ...

  5. Android 自定义CheckBox样式

    1.首先在drawable文件夹中添加drawable文件checkbox_style.xml. <selector xmlns:android="http://schemas.and ...

  6. Android中自定义ActionBar的背景色等样式style

    Android中想要去自定义ActionBar的背景色等样式. [折腾过程] 1.自己找代码,发现对应的配置的地方了: AndroidManifest.xml ? 1 2 <applicatio ...

  7. 自定义 checkbox 样式

    前言:最近在做一个网站,为了统一风格,需要自定义checkbox的样式.所以花了点时间参考了 研究了一下.感觉上面的方法略微麻烦.所以自己重新写了下面的代码,欢迎大家指教.同时,感谢w3cplus提供 ...

  8. WPF 自定义CheckBox样式

    自定义CheckBox样式,mark一下,方便以后参考复用 设计介绍: 1.一般CheckBox模板太难看了,肯定要重写其中的模板 2.模板状态为未选中状态和选中状态,设置为默认未选中就好了. 默认状 ...

  9. 转--Android中自定义字体的实现方法

    1.Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace 2.在Android中可以引入其他字体 . 复制代码 代码如下: <?xml versio ...

随机推荐

  1. 在Oracle中查询表的大小

    SELECT segment_name AS TABLENAME,round(BYTES/1024/1024,2)  FROM user_segments WHERE segment_name='表名 ...

  2. python基础===string模块常量

    In [8]: import string In [9]: dir(string) In [10]: string.ascii_letters Out[10]: 'abcdefghijklmnopqr ...

  3. 64_o2

    openrdf-sesame-queryrender-2.8.10-2.fc26.noarch..> 11-Feb-2017 18:38 52014 openrdf-sesame-queryre ...

  4. 使AJAX可缓存——基于flask

    主流浏览器都有缓存机制,主要基于HTTP协议定义的缓存策略.对于一定时间内不发生变动的文档缓存起来,对于下次请求,就可以直接返回缓存的结果.使用缓存有以下好处: 1.减少冗余的数据传输,节省网络流量成 ...

  5. Builder设计模式--改善构造器多个参数时可显著改善可读性

    作为一名程序开发者,设计模式其实一直有在接触,只是没有专门的去学过,所以可能对设计模式没有一个系统的理解.在一次项目中,需要使用到第三方服务商提供的功能,为了尽快的熟悉其功能代码,在官网下了demo来 ...

  6. nginx解析带中文的url重定向之后404问题

    首先,有这样一个需求:一个系统的图片存储是放在upyun上的,现在有个客户需要本地化部署,所以需要将图片进行本地存储.为了兼容,图片存储的路径格式保持和在upyun上的存储路径一致.在upyun上的存 ...

  7. day2 字典常用的方法

        字典创建的方式: (1)d1 = {"k1":"v1","k2":"v2","k3":&qu ...

  8. Java 深入浅出String

    String String是一个被final修饰的类,直接继承于Object,同时也实现了charsequence接口,String被声明为final也就不可以被继承了.由于String的方法比较多, ...

  9. enumerate()和map()函数用法

    一.python enumerate用法 先出一个题目: 1.有一 list= [1, 2, 3, 4, 5, 6] 请打印输出: 0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输出 ...

  10. python爬虫实战(六)--------新浪微博(爬取微博帐号所发内容,不爬取历史内容)

    相关代码已经修改调试成功----2017-4-13 详情代码请移步我的github:https://github.com/pujinxiao/sina_spider 一.说明 1.目标网址:新浪微博 ...