Android中的风格和主题(style和theme)

style 和 theme 其实在意义上没有什么不同,他们都是一组UI属性的定义集合。而他们中间的区别就是作用的范围而已,还有一些在 theme 可以设置但是在 style 不能设置的属性。


一.style 和 theme 的区别

因为开始使用 style 和 theme 时一般比较困惑他俩到底有什么不同,所以在这里就先介绍一下 style 和 theme 的不同吧:

style: 针对一些特定的具体的组件,例如TextView、EditText、ImageButton等。他们都是某个 activity 中的控件

theme:  用来设置 activity 或 application 的样式,如果设置了的话,那么这个 activity 或者整个儿 app 中的控件都会使用 theme 中定义的UI属性



二.style 和 theme 的创建

style 和 theme 的创建方法都是一样的,只是引用他们的时候用的xml标签不一样,所以这里以style 的创建方法做样例,至于引用的区别下边也会给出。

style和theme都是作为一种资源文件而存在的,所以要把内容写在xml文件中并放在工程目录中的 res/values/ 目录下,定义 style 会用到如下几个标签:

(1)<resources>: 这个标签必须作为该style xml文件的根标签

(2)<style>: 这个标签必须在<resources>标签的内部,用来定义style,可以设置这个标签的name属性来定义style的标识符,也就是说你要用这个style的时候要通过name属性的值来找到并引用它。

(3)<item>: 这个标签必须在<style>标签的内容,用来定义相应属性的值,例如我们最常见的android:layout_width可以被这么定义<item name="android:layout_width">fill_parent</item>。


下面给出一个样例文件,文件路径+文件名为 res/values/mystyle.xml:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="customStyle">
        <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:textSize">24sp</item>
    </style>
</resources>

三.style 和 theme 的使用

那么定义好的 style 如何使用呢?可以在需要用到的地方这样设置它,以一个TextView控件来示范:

<TextView
    style="@style/customStyle"     <!-- customStyle就是上面mystyle.xml中style标签里的name的值 -->
    android:text="@string/hello_world" />

如果想要把它用作 theme,那么就需要在 AndroidManifest.xml 的 <application> 或 <activity> 标签中来设置:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/customStyle" >
    ……
</application>
<activity
    android:name="com.example.testapp.MainActivity"
    android:theme="@style/customStyle"
    android:label="@string/app_name" >
    ……
</activity>


四.继承的使用

如果你并不想要完全重新定义一组UI,而是希望扩展之前实现过的或是SDK自带的一套 style 和 theme ,那么就可以使用接下来这种继承的方法。

继承的方式有两种:

(1)  通过设置<style>标签中的 parent 属性来指定父style:

<style name="customStyle">
    <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:textSize">24sp</item>
</style>
    
<style name="customStyle2" parent="@style/customStyle">  <!-- 通过parent属性来指定父style -->
    <item name="android:textColor">#FF0000</item>
</style>

(2) 通过给<style>中的name属性添加前缀来指定父style:

<style name="customStyle">
    <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:textSize">24sp</item>
</style>
    
<style name="customStyle.customStyle2">   <!-- 通过添加前缀来指定父style -->
    <item name="android:textColor">#FF0000</item>
</style>

注意1:派生的style中定义的属性会覆盖父style中已经定义了的属性

注意2:如果是继承SDK自带的style或者theme,那么就必须使用设置parent属性的方法来实现,不能用加前缀的方法



如果转载请注明出处:http://blog.csdn.net/gophers/article/details/21119667

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现简单的主题风格,可以使用 Android studio 提供的资源文件和样式表来定义组件的外观和行为。以下是实现自定义样式的步骤: 1. 在 res/values 目录下创建一个新的 styles.xml 文件,用于定义样式表。 2. 在 styles.xml 文件定义一个基础样式,例如: ``` <style name="MyButtonStyle" parent="Widget.AppCompat.Button"> <item name="android:textColor">#FFFFFF</item> <item name="android:background">#FF0000</item> </style> ``` 这里创建了一个名为 MyButtonStyle 的样式,继承自 AppCompat 库的 Button 组件,并设置了文本颜色和背景颜色。 3. 在 styles.xml 文件定义一个主题,例如: ``` <style name="MyTheme" parent="Theme.AppCompat.Light"> <item name="android:colorPrimary">#FF0000</item> <item name="android:colorPrimaryDark">#990000</item> <item name="android:textColorPrimary">#FFFFFF</item> <item name="android:textColorSecondary">#CCCCCC</item> <item name="android:windowBackground">#FFFFFF</item> <item name="android:buttonStyle">@style/MyButtonStyle</item> </style> ``` 这里创建了一个名为 MyTheme主题,继承自 AppCompat 库的 Light 主题,并设置了主题色、文本颜色、窗口背景色和按钮样式等属性。 4. 在 AndroidManifest.xml 文件指定使用该主题,例如: ``` <application android:theme="@style/MyTheme" ...> ... </application> ``` 这里将应用程序的主题设置为 MyTheme。 5. 在布局文件使用自定义样式,例如: ``` <Button android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/MyButtonStyle" android:text="My Button" /> ``` 这里将 Button 组件的样式设置为 MyButtonStyle。 通过以上步骤,就可以实现简单的主题风格,并给组件自定义样式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值