Android使用者介面 UI組件–TextView及其子類(二) Button,selector選取器,sharp屬性

來源:互聯網
上載者:User

1.XML檔案中的OnClick 屬性可以指定在Activity中處理點擊事件的方法,Activity中必須定義該屬性指定的值作為方法的名字且有一個View類型的參數,表示此物件被點擊。


2.使用selector配置可以實現不同模式下按鈕的樣式。


3.使用shape配置可以定製各種不同的按鈕樣式。


        selector檔案屬性說明:

android:drawable 預設狀態
android:state_pressed 是否按下,如一個按鈕觸摸或者點擊。
android:state_focused 是否取得焦點,比如使用者選擇了一個文字框。
android:state_hovered 游標是否懸停,通常與focused state相同,它是4.0的新特性
android:state_selected 被選中,它與focus state並不完全一樣,如一個list view 被選中的時候,它裡面的各個子組件可能通過方向鍵,被選中了。
android:state_checkable 組件是否能被check。如:RadioButton是可以被check的。
android:state_checked 被checked了,如:一個RadioButton可以被check了。
android:state_enabled 能夠接受觸摸或者點擊事件
android:state_activated 被啟用(這個麻煩舉個例子,不是特明白)
android:state_window_focused 應用程式是否在前台,當有通知欄被拉下來或者一個對話方塊彈出的時候應用程式就不在前台了

注意:如果有多個item,那麼程式將自動從上到下進行匹配,最先匹配的將得到應用。(不是通過首選)
如果一個item沒有任何的狀態說明,那麼它將可以被任何一個狀態匹配。


shape屬性說明:

solid:實心,就是填充的意思
android:color指定填充的顏色

gradient:漸層
android:startColor和android:endColor分別為起始和結束顏色,ndroid:angle是漸層角度,必須為45的整數倍。
另外漸層預設的模式為android:type="linear",即線性漸層,可以指定漸層為放射狀漸層,android:type="radial",放射狀漸層需要指定半徑android:gradientRadius="50"。

stroke:描邊
android:width="2dp" 描邊的寬度,android:color 描邊的顏色。
我們還可以把描邊弄成虛線的形式,設定方式為:
android:dashWidth="5dp" 
android:dashGap="3dp"
其中android:dashWidth表示'-'這樣一個橫線的寬度,android:dashGap表示之間隔開的距離。

corners:圓角
android:radius為角的弧度,值越大角越圓。
我們還可以把四個角設定成不同的角度,方法為:
<corners 
        android:topRightRadius="20dp"    右上方
        android:bottomLeftRadius="20dp"    右下角
        android:topLeftRadius="1dp"    左上方
        android:bottomRightRadius="0dp"    左下角
 />
這裡有個地方需要注意,bottomLeftRadius是右下角,而不是左下角
padding:間隔
這個就不用多說了,XML布局檔案中經常用到。


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >       <EditText android:layout_width="match_parent"              android:layout_height="wrap_content"              android:inputType="text"/>     <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/button_selector"        android:onClick="onBtnClick"        android:text="This is a Button."        tools:ignore="HardcodedText" /></LinearLayout>

package com.light.study.android;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}    public void onBtnClick(View view){    Toast.makeText(this, "you are so clever!", Toast.LENGTH_LONG).show();    }}

selector檔案:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- 沒有焦點時的背景圖片-->    <item android:state_window_focused="false" android:drawable="@drawable/button_normal_style" />    <!-- 觸摸模式下單擊時的背景圖片-->    <item android:state_focused="true" android:state_pressed="true"          android:drawable="@drawable/button_press_style"/>    <!-- 非觸摸模式下獲得焦點並單擊時的背景圖片-->    <item android:state_focused="false" android:state_pressed="true"           android:drawable="@drawable/button_press_style"></item> </selector>

2個shape檔案

按鈕按下:

<?xml version="1.0" encoding="utf-8"?><shape  xmlns:android="http://schemas.android.com/apk/res/android" >            <!-- 實心 -->            <solid android:color="#ff9d77"/>            <!-- 漸層 -->            <gradient                android:startColor="#ff8c00"                android:endColor="#FFFFFF"                android:angle="270" />            <!-- 描邊 -->            <stroke                android:width="2dp"                android:color="#dcdcdc" />            <!-- 圓角 -->            <corners                android:radius="2dp" />            <padding                android:left="10dp"                android:top="10dp"                android:right="10dp"                android:bottom="10dp" /></shape>


正常狀態:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >    <solid  android:color="#f4100f"/>    <stroke android:width="2dp"            android:color="#ccc" />    <corners android:radius="8dp" /></shape>

結果:


點擊後:





聯繫我們

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