【android开发】CheckBox加载自定义选中与未选中图片样式
2013-09-25 11:33
721 查看
在很多项目中我们不想用到android自带的样式,想把一个自定义样式显示在窗口上,这样一面是符合特殊需求,另一面会显得更加的有个性和灵活性。在这里单独讲CheckBox的自定义样式的实现,对于其他按钮实现的原理是一样。先上图看看效果
我选择的两张图片样式比较丑,大家ps几张漂亮的。下面介绍相关实现原理及过程:
知道其中的原理,再去自己尝试是最好的方式,我们所有的修改都是在源码的基础好修改的。
打开源码中CheckBox.java文件,我们可以看到如下内容:
public class CheckBox extends CompoundButton {
public CheckBox(Context context) {
this(context, null);
}
public CheckBox(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.checkboxStyle);
}
public CheckBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
可以看到,CheckBox是继承于CompoundButton,而CompoundButton:
public abstract class CompoundButton extends Button implements Checkable
是Button的一个子类,由此可知,CheckBox也为Button的一个子类。从上面的代码中可以看出,CheckBox的不同之处就是添加了属性checkboxStyle,我们在源码中找到该样式的定义如下:
<style name="Widget.CompoundButton.CheckBox">
<item name="android:background">@android:drawable/btn_check_label_background</item>
<item name="android:button">@android:drawable/btn_check</item>
</style>
其中,在资源文件中找到btn_check_label_background,该图片是背景透明没有内容的一张图片,因此CheckBox的显示就是由btn_check决定,我们打开btn_check.xml文件,可以看到以下内容:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Enabled states -->
<item android:state_checked="true" android:state_window_focused="false"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on" />
<item android:state_checked="false" android:state_window_focused="false"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off" />
<item android:state_checked="true" android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on_pressed" />
<item android:state_checked="false" android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off_pressed" />
<item android:state_checked="true" android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on_selected" />
<item android:state_checked="false" android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off_selected" />
<item android:state_checked="false"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off" />
<item android:state_checked="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on" />
<!-- Disabled states -->
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="@drawable/btn_check_on_disable" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="@drawable/btn_check_off_disable" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/btn_check_on_disable_focused" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/btn_check_off_disable_focused" />
<item android:state_checked="false" android:drawable="@drawable/btn_check_off_disable" />
<item android:state_checked="true" android:drawable="@drawable/btn_check_on_disable" />
</selector>
每一个item就对应着CheckBox在不同的选择状态下显示的效果,了解该文件的内容后,我们就可以更具自己的需要来设计美观的CheckBox。RadioButton的显示与CheckBox基本相似,可以仿照CheckBox加以修改。
具体实现过程大家参考一下:
1.在drawable中创建文件checkbox_selectors.xml
具体代码如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false"
android:state_pressed="true"
android:drawable="@drawable/grid_check_off_press"></item>
<item android:state_checked="false"
android:drawable="@drawable/grid_check_off"></item>
<item android:state_checked="true"
android:state_pressed="true"
android:drawable="@drawable/grid_check_on_press" />
<item android:state_checked="true"
android:drawable="@drawable/grid_check_on"></item>
</selector>
2.在values中styles.xml中添加一下代码:
<style name="mycheckbox"
parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/checkbox_selectors</item>
<item name="android:paddingLeft">10.0dip</item>
<item name="android:height">35.0dip</item>
</style>
3.在你的layout文件中添加CheckBox中添的属性:
<CheckBox
android:id="@+id/checkBox1"
style="@style/mycheckbox"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignLeft="@+id/tv"
android:layout_below="@+id/tv"
android:layout_marginTop="84dp"
android:text="自定义样式" />
到此所有的工作就完成了,你可以进行测试一下了!
由于写的属于最基础的,没有作具体的注释,欢迎提出意见进行交流!
实例源码下载地址http://download.csdn.net/detail/lixinhuixin/6318717
我选择的两张图片样式比较丑,大家ps几张漂亮的。下面介绍相关实现原理及过程:
知道其中的原理,再去自己尝试是最好的方式,我们所有的修改都是在源码的基础好修改的。
打开源码中CheckBox.java文件,我们可以看到如下内容:
public class CheckBox extends CompoundButton {
public CheckBox(Context context) {
this(context, null);
}
public CheckBox(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.checkboxStyle);
}
public CheckBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
可以看到,CheckBox是继承于CompoundButton,而CompoundButton:
public abstract class CompoundButton extends Button implements Checkable
是Button的一个子类,由此可知,CheckBox也为Button的一个子类。从上面的代码中可以看出,CheckBox的不同之处就是添加了属性checkboxStyle,我们在源码中找到该样式的定义如下:
<style name="Widget.CompoundButton.CheckBox">
<item name="android:background">@android:drawable/btn_check_label_background</item>
<item name="android:button">@android:drawable/btn_check</item>
</style>
其中,在资源文件中找到btn_check_label_background,该图片是背景透明没有内容的一张图片,因此CheckBox的显示就是由btn_check决定,我们打开btn_check.xml文件,可以看到以下内容:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Enabled states -->
<item android:state_checked="true" android:state_window_focused="false"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on" />
<item android:state_checked="false" android:state_window_focused="false"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off" />
<item android:state_checked="true" android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on_pressed" />
<item android:state_checked="false" android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off_pressed" />
<item android:state_checked="true" android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on_selected" />
<item android:state_checked="false" android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off_selected" />
<item android:state_checked="false"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off" />
<item android:state_checked="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on" />
<!-- Disabled states -->
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="@drawable/btn_check_on_disable" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="@drawable/btn_check_off_disable" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/btn_check_on_disable_focused" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/btn_check_off_disable_focused" />
<item android:state_checked="false" android:drawable="@drawable/btn_check_off_disable" />
<item android:state_checked="true" android:drawable="@drawable/btn_check_on_disable" />
</selector>
每一个item就对应着CheckBox在不同的选择状态下显示的效果,了解该文件的内容后,我们就可以更具自己的需要来设计美观的CheckBox。RadioButton的显示与CheckBox基本相似,可以仿照CheckBox加以修改。
具体实现过程大家参考一下:
1.在drawable中创建文件checkbox_selectors.xml
具体代码如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false"
android:state_pressed="true"
android:drawable="@drawable/grid_check_off_press"></item>
<item android:state_checked="false"
android:drawable="@drawable/grid_check_off"></item>
<item android:state_checked="true"
android:state_pressed="true"
android:drawable="@drawable/grid_check_on_press" />
<item android:state_checked="true"
android:drawable="@drawable/grid_check_on"></item>
</selector>
2.在values中styles.xml中添加一下代码:
<style name="mycheckbox"
parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/checkbox_selectors</item>
<item name="android:paddingLeft">10.0dip</item>
<item name="android:height">35.0dip</item>
</style>
3.在你的layout文件中添加CheckBox中添的属性:
<CheckBox
android:id="@+id/checkBox1"
style="@style/mycheckbox"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignLeft="@+id/tv"
android:layout_below="@+id/tv"
android:layout_marginTop="84dp"
android:text="自定义样式" />
到此所有的工作就完成了,你可以进行测试一下了!
由于写的属于最基础的,没有作具体的注释,欢迎提出意见进行交流!
实例源码下载地址http://download.csdn.net/detail/lixinhuixin/6318717
相关文章推荐
- Android中自定义加载样式图片的具体实现
- Android ProgressBar 自定义样式(二),仿原来新浪微博图片加载样式
- Android中实现加载样式图片自定义
- Android开发 图片轮播条Lanner(可无限滚动循环,自定义dots样式位置,延迟时间,滚动时间,点击事件)
- Android自定义View基础开发之图片加载进度条
- Android ProgressBar 自定义样式(七),仿真QQ聊天、网易新闻客户端加载图片Loading
- Android ProgressBar 自定义样式(七),仿真QQ聊天、网易新闻客户端加载图片Loading
- Android广告图片轮播控件,支持无限循环和5种主题,可以灵活设置轮播样式、时间、位置、图片加载框架等!
- Android自定义ProgressDialog加载图片
- Android开发技巧——TextView加载HTML的图片及代码显示问题
- Android中自定义checkbox样式
- Android 自定义CheckBox样式
- 【iOS开发-62】自定义cell制作团购页面、顶部图片轮播、底部模拟加载更多功能,核心是练习代理模式
- Android开发中如何解决加载大图片时内存溢出的问题
- Android开发之自定义圆角矩形图片ImageView
- android 开发中常用的第三方库(图片加载篇——Glide)
- android checkbox 未选中状态 已选中状态 替换成自己的图片
- [转]Android中自定义checkbox样式
- 【android精选】图片涂鸦下拉加载地图大集合五子棋游戏自定义上下文菜单源码
- android开发游记:自定义图片裁剪组件