2013年10月22日 星期二

不知道要怎麼翻譯的selector

同一種效果,有很多種寫法,寫法越簡單越好,selector就是個簡單的東西,只要有個xml就搞定了。

1. 建立專案

2. 加個ImageButton



3. 在/res/drawable-mdpi目錄下建立myselector.xml


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

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

    <item

        android:drawable="@drawable/red"

        android:state_pressed="false" />

    <item

        android:drawable="@drawable/green"

        android:state_pressed="true" />

</selector>

4. /res/layout/activity_main.xml的ImageButton要指定myselector


<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:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >


    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/hello_world" />


    <ImageButton

        android:id="@+id/imageButton1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/textView1"

        android:layout_centerHorizontal="true"

        android:src="@drawable/myselector" />


</RelativeLayout>


5. 一開始的畫面

6. 給它按下去,放開就又變紅的喔!

寫個xml,一個selector就可以搞定,java一個字都不用改,簡單吧!當然還不只這些功能,花些時間看看Drawable Resources,光是selector就還有一堆屬性可以選用,玩玩看吧!!!


標籤: ,

0 個意見:

張貼留言

<< 首頁