您的位置:首页 > 移动开发 > Android开发

【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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐