(转)使用Android标签属性style

转自: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/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值