android中自訂checkbox大小和圖片

來源:互聯網
上載者:User

在編程過程中使用android內建的checkbox顯示過大,在網上找了很多文章,終於使用自訂的checkbox使顯示更加美觀。

 

網上說:這個控制項其實就是個TextView加了個圖片,你只要做兩張png的圖片,在darwable中用xml定義好點擊事件,再在你的控制項上把這個當背景引進來就可以了。但是這樣做了以後顯示效果還是不佳。說說我的做法吧:

1、找兩張圖片http://findicons.com/search/checkbox#  分別為選中和沒選中的。命名為checkbox和checkbox_empty

2、在drawable中建立檔案checkbox_selector.xml:

<?xml version="1.0" encoding="utf-8"?><br /><selector xmlns:android="http://schemas.android.com/apk/res/android"><br /><item android:state_checked="true"<br /> android:drawable="@drawable/checkbox" /><!--選中時效果--><br /><item android:state_checked="false"<br /> android:drawable="@drawable/checkbox_empty" /><!--未選中時效果--><br /> <!-- 修改成你自己的圖片就可以了 --><br /></selector> 

注意:這裡的狀態是android:state_checked

3、在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>

4、在你的CheckBox中添加屬性:

<CheckBox<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> style="@style/MyCheckBox"<br /> /> 

經過以上步驟應該可以了。我對style和selector的使用也不熟悉,大家一起學習~

 

這裡查過的資料有:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=70428

http://topic.csdn.net/u/20101030/12/705ede36-c873-4d07-af0d-34c6b9145480.html

http://blog.sina.com.cn/s/blog_7898b0530100rfgs.html

最後上個圖吧:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.