现在的位置: 首页 > 综合 > 正文

(转)使用Android标签属性style

2013年10月03日 ⁄ 综合 ⁄ 共 1697字 ⁄ 字号 评论关闭

转自:http://henzil.easymorse.com/?p=169#more-169

 

在Android的编程中,定义的一个按钮的点中,获得焦点等一些状态时,各个状态使用不同的图片,如下在drawable目录下定义了一个Button的各种状态时的样式,btn_blue.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/btn_blue_normal">
    </item>
    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/btn_blue_normal_disable">
    </item>
    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/btn_blue_selected">
    </item>
    <item android:state_pressed="true" android:drawable="@drawable/btn_blue_pressed">
    </item>
    <item android:state_enabled="true" android:drawable="@drawable/btn_blue_normal">
    </item>
    <item android:state_focused="true"
        android:drawable="@drawable/btn_blue_normal_disable_focused">
    </item>
    <item android:drawable="@drawable/btn_blue_normal_disable">
    </item>
</selector>

这里用到了一些按钮状态图片,然后在main.xml文件中定义一个Button,

<Button android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    style="@style/ButtonBlue"
    android:text="按钮"/>

这里用到了style属性。如果这样写style="@drawable/btn_blue"时,是显示不出我们自定义的效果的。

在values目录下,新建attrs.xml。在其中定义:

<resources>

    <style name="ButtonBlue" >
        <item name="android:background">@drawable/btn_blue</item>
    </style>

注意item name="android:background"这里我各个状态主要是改变其背景色,所以这里我写的name="android:background"。在item标签中加载drawable目录下的btn_blue.xml文件。这里<style name="ButtonBlue" >
这里的name是自己写名字,然后在main.xml文件中style属性如下:style="@style/ButtonBlue"

源码:http://henzil.googlecode.com/svn/trunk/buttonDemo/

抱歉!评论已关闭.