Android 中style的使用

Style的作用简单讲就是使布局中的一些控件有相同的显示风格,而且不用重复的在控件中定义他们的属性。

或者说对控件的一种格式化的输出,使功能相近的控件有一致的对外表现形式,而且不用重复的复制代码。


使用Style
在控件的定义中添加 :style=”@style/btn_style_1”
同时在文件夹 res/values/style.xml 添加一个 btn_style_1项 即可。

例如: Button 中使用了style=”@style/btn_style_1”的属性

        <Button
            android:id="@+id/btn4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"
            android:focusable="true"
            android:text="Test 4" 
            android:layout_weight="1"
            style="@style/btn_style_1"/>

同时styles.xml中给出btn_style_1的定义,其中也是定义了一下控件的属性,如果其他的控件需要和Button 1有一样的显示风格就可以直接使用了。

<resources>
    <style name="btn_style_1">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#ffffff</item>
        <item name="android:gravity">center</item>
        <item name="android:textStyle">italic</item>
        <item name="android:shadowColor">#ffffff</item>
        <item name="android:shadowDx">1</item>
        <item name="android:shadowDy">1</item>
        <item name="android:shadowRadius">5</item>
        <item name="android:background">@drawable/btn_style_1_selector</item>
    </style>
</resources>    

注: 其中 android:background属性中用到了 btn_style_1_selector,它是定义在 res/drawable文件夹中的一个xml 文件,它可以描述当按钮按下时和正常时的显示风格。
例如: 它可以在按钮按下时/正常时 显示不同的图片。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_add_accounts_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_add_accounts_normal"/>

</selector>

这里总结了一些按钮的style,以备开发时使用。

这里写图片描述

工程代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值