Android開發CheckBox自訂圖片問題

來源:互聯網
上載者:User

一,CheckBox自訂圖片問題

結果點擊的時候 會有checked效果,但是,手指離開後沒有check住~

 代碼如下 複製代碼

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_checked="false"
android:drawable="@drawable/checkbox_tishi" />
<item android:state_checked="true"
android:drawable="@drawable/checkbox_tishi_select" />

</selector>

上面的寫法,最後當然沒好使,於是就搜尋了一下,搜來的多了一對標籤狀態,但還是不能用。但是原生的是可以的,想了想還是看看源碼好了。發現源碼中的配置多了不少,當然首先就是全部複製過來。然後,適當精簡一下。再次發布程式,頓時好使了。結果如下:

 代碼如下 複製代碼

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_checked="true" android:state_window_focused="false"
android:drawable="@drawable/checkbox_tishi_select" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="@drawable/checkbox_tishi" />

<item android:state_checked="true" android:state_pressed="true"
android:drawable="@drawable/checkbox_tishi_select" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="@drawable/checkbox_tishi" />

<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/checkbox_tishi_select" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/checkbox_tishi" />

<item android:state_checked="false"
android:drawable="@drawable/checkbox_tishi" />
<item android:state_checked="true"
android:drawable="@drawable/checkbox_tishi_select" />

</selector>


二,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" 
        />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.