Android样式——Styles

说明

样式(style)是属性的集合,用来指定View或者Window的外观和格式。

这些属性可以是height(高度)、padding(内边距)、font size(字体颜色)等。

样式定义在另一个xml文件中,从布局文件中分离出来。

例如:

1 <TextView
2     android:layout_width="fill_parent"
3     android:layout_height="wrap_content"
4     android:textColor="#00FF00"
5     android:typeface="monospace"
6     android:text="@string/hello" />

如果使用样式,则可以转换为

1 <TextView
2     style="@style/CodeFont"
3     android:text="@string/hello" />

CodeFont就是样式文件名,把一些属性从原布局文件中提取出来,存放到了CodeFont文件中。

定义

样式文件必须存放在res/valuse的文件夹中,命名任意,后续为.xml。

CodeFont.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3     <style name="CodeFont" parent="@style/BaseFont">
4         <item name="android:layout_width">fill_parent</item>
5         <item name="android:layout_height">wrap_content</item>
6         <item name="android:textColor">#00FF00</item>
7         <item name="android:typeface">monospace</item>
8     </style>
9 </resources>

 <resources>:根元素。

<style>:属性集合。

<item>:属性。

使用parent,可以继承样式。除此之外,还可以在<style>的命名前添加"继承样式名.",效果一样。例如:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="BaseFont.CodeFont" >
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
    </style>
</resources>

 注意:

引用此样式时的名字为BaseFont.CodeFont。

如果引用系统自带的样式,值的格式为“@android:style/样式名”。我们自定义的样式,值的格式为“@style/样式名”。

应用

两种方式

对单独的View,举例:

  布局文件中,<TextView style="@style/CodeFont" android:text="@string/hello" />

对整个Activity或整个应用程序,举例:

  AndroidManifest.xml文件中,<activity android:theme="@android:style/Theme.Dialog">或者<application android:theme="@style/CustomTheme">

 

参考:http://developer.android.com/guide/topics/ui/themes.html

 

转载于:https://www.cnblogs.com/dann/p/3223947.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值