Android開發學習筆記(六)Android應用介面編程 Button/ImageButton學習

來源:互聯網
上載者:User

ImageButton繼承自ImageView,Button繼承自View。

相比ImageButton,Button具有更強大的功能。

ImageButton僅僅可以指定一個圖片(也可是xml),沒有文字,而Button圖片文字都可以指定。

本測試使用了四個按鈕,分別是普通按鈕btn1;有文字有背景圖(按下時候有變換效果)的按鈕btn2;普通圖片按鈕btn3;有變換效果的圖片按鈕btn4.

測試代碼:

main.xml

<RelativeLayout 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" >    <Button        android:id="@+id/btn1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="press it" />    <Button        android:id="@+id/btn2"        android:layout_below="@id/btn1"        android:layout_centerHorizontal="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="exit"        android:background="@drawable/button_sel" />        <ImageButton        android:id="@+id/btn3"        android:layout_below="@id/btn2"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:src="@drawable/one"         android:background="#ffffff"/>    <ImageButton        android:id="@+id/btn4"        android:layout_below="@id/btn3"        android:layout_centerHorizontal="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/button_sel" /></RelativeLayout>

button_sel.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/one"/><item android:state_pressed="false"  android:drawable="@drawable/two"/></selector>

: 

初始狀態:                                                                        點擊第二個按鈕exit之後:                                  點擊第四個按鈕之後:

                             

聯繫我們

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