[转]Android中自定义checkbox样式

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。

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:drawable="@drawable/checkbox_pressed" android:state_checked="true"/>  
  5.     <item android:drawable="@drawable/checkbox_normal" android:state_checked="false"/>  
  6.     <item android:drawable="@drawable/checkbox_normal"/>  
  7.   
  8. </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 自定义CheckBoxPreference的CheckBox复选框

在使用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.   
  4.     <CheckBoxPreference  
  5.         android:key="cbp"  
  6.         android:summaryOff="Off"  
  7.         android:summaryOn="On"  
  8.         android:title="CheckBoxPreference"  
  9.         android:widgetLayout="@layout/my_checkbox" />  
  10.   
  11. </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.   
  4.     <item android:drawable="@drawable/icon_checkbox_unchecked" android:state_checked="false"/>  
  5.     <item android:drawable="@drawable/icon_checkbox_checked" android:state_checked="true"/>  
  6.   
  7. </selector>  

4.MainActivity.java注意要继承PreferenceActivity

    1. public class MainActivity extends PreferenceActivity {  
    2.   
    3.     @Override  
    4.     public void onCreate(Bundle savedInstanceState) {  
    5.         super.onCreate(savedInstanceState);  
    6.         addPreferencesFromResource(R.xml.my_preference);  //这个方法已过时!
    7.     }  
    8.   
    9.     @Override  
    10.     public boolean onCreateOptionsMenu(Menu menu) {  
    11.         getMenuInflater().inflate(R.menu.activity_main, menu);  
    12.         return true;  
    13.     }  
    14.       
原文地址:https://www.cnblogs.com/ZhuRenWang/p/4856143.html