android设置修改CheckBox框大小、颜色

 

 

首先:大小由java代码来设置;颜色由图片来决定;
注意:如果有人的checkbox太大或太小,应该是选择的drawable文件夹不对,我这个是大尺寸的,放在了drawable-xxhdpi下。如果你用自己的图片,请根据美工做图的大小来放到相应的drawable下。
准备:两张用作CheckBox选中和没有选中状态的图片到res的drawable-xxhdpi中,名称可以设置如下:

【第一种简单的方法:】

1、在res/drawable-xxhdpi中添加checkbox_style.xml,用于定义checkbox的drawable图片。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/checkbox_checked" 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文件中添加MyCheckBox样式

<style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">  
    <item name="android:button">@drawable/checkbox_style</item>  
</style>

3、在layout布局文件中使用MyCheckBox样式

<CheckBox  
        android:id="@+id/select_all"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        style="@style/MyCheckBox" />

【第二种稍微复杂的方法:】
注:关键点在于设置
android:button="@null"
以及
drawable.setBounds(0,0,40,40);
checkBox.setCompoundDrawables(drawable,null,null,null);

 

 

四、在Activity中的Java代码中设置大小和位置

//取得CheckBox对象
CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);

//取得设置好的drawable对象
Drawable drawable = this.getResources().getDrawable(R.drawable.checkbox_style);

//设置drawable对象的大小
drawable.setBounds(0,0,40,40);

//设置CheckBox对象的位置,对应为左、上、右、下
checkBox.setCompoundDrawables(drawable,null,null,null);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值