安卓天天练练(四)drawable state 属性

今天又作茧自缚(item 写成 itme ╮(╯▽╰)╭ elipse还自动闭合了标签,来回查查查看报错,为啥点击无效呢!)

真欠抽,怪不得上班地铁上被个sb踢到脚趾头(目测有可能是同家公司的..同站下车他直奔中行ATM,西楼面熟)

在drawable下建的xml,IDE选selector类型

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

layout中的主文件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: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="com.narumi.android_7_3.MainActivity" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/mybutton"  
        android:background="@drawable/btn"
         />

</RelativeLayout>

一行java也不用写,p了两张图片,此例结束。

备注:

android:state_pressed 是否按下,如一个按钮触摸或者点击。
android:state_focused 是否取得焦点,比如用户选择了一个文本框。
android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_selected 被选中,它与focus并不相同,如一个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没有任何的状态说明,那么它将可以被任何一个状态匹配。

原文地址:https://www.cnblogs.com/haimingpro/p/4666915.html