android控制項布局 ToggleButton按鈕 圖片和文字

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   使用   io   2014   

ToggleButton控制項具有checked屬性對於要切換狀態的功能按鈕來說十分好用

這裡以phone的開啟DTMFTwelveKeyDialer的按鈕為例 展示其效果和布局

 

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >       <ToggleButton           android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@drawable/button_incall"            android:drawableTop="@drawable/icon_keyboard_bg"            android:textOn="鍵盤"            android:textOff="鍵盤"            android:textSize="32dip"            android:textColor="#FFFFFF"        />        </LinearLayout>

其中drawableTop引用了icon_keyboard_bg.xml

icon_keyboard_bg.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">        <item android:state_pressed="true"        android:drawable="@drawable/icon_keyboard_up_pressed"        />        <item android:state_checked="true"        android:drawable="@drawable/icon_keyboard_up"        />        <item android:state_checked="false"        android:drawable="@drawable/icon_keyboard_down"        />        <item        android:drawable="@drawable/icon_keyboard_down"        /></selector>

這樣 即可實現點擊按鈕時三種不同狀態圖片的切換了

 

1. 自然狀態下效果

 

2.按下按鈕效果

 

3.屬性checked="true"的效果

 

實際使用中應該注意國際化的問題  字串、間距、尺寸的大小不應使用寫入程式碼

聯繫我們

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