自訂ToggleButton的樣式,togglebutton樣式

來源:互聯網
上載者:User

自訂ToggleButton的樣式,togglebutton樣式

1.案例

2.準備相關的資源

2.1準備圖片(一般放在drawable-hdpi目錄下)

3.樣式檔案(toggle_button.xml)

3.1樣式檔案位置通常在drawable目錄下定義

3.2樣式代碼

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

<!-- 選中樣式 -->

<item android:drawable="@drawable/toggle_btn_checked" android:state_checked="true"/>

<!-- 未選中樣式 -->

<item android:drawable="@drawable/toggle_btn_unchecked" android:state_checked="false"/>

</selector>

4.應用樣式(main.xml)

<ToggleButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@color/transparent"

android:button="@drawable/toggle_button"

android:text=""

android:textOff=""

android:textOn="" />

4.1 為了只顯示圖片需要設定text、textOff、textOn屬性的內容為空白字串

4.2 為了充分顯示出圖片,背景設定為透明的顏色 #0000000

4.3 為了顯示圖片按鈕,在button屬性上應用樣式

4.4 控制項的寬度和高度屬性設定為"wrap_content",圖片資源最好放在drawable-hdpi目錄下才能完全顯示圖片(高解析度下)

聯繫我們

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