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換成你設置的那兩張圖片了

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM