Android中自定义样式(style)与主题(theme)

Android提供了许多可视的组件。通过自定义样式和主题,可以避免用这些组件开发的应用看上去千篇一律。

样式和主题都是通过预定义一系列属性值来形成统一的显示风格。区别是,样式只能应用于某种类型的View;而主题刚好相反,它不能应用于特定的View,而只能作用于一个或多个Activity,或是整个应用。

以下结合具体例子说明如何定义样式和主题:

1.定义样式和主题
在工程中res/values/下添加styles.xml


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <!-- 定义my_style_1,没有指定parent,用系统缺省的 -->
  4. <style name="my_style_1">
  5. <!-- 定义与指定View相关的若干属性 -->
  6. <item name="android:hint">load from style 1</item>
  7. </style>

  8. <!-- 定义my_style_2,用自定义的my_style_1作为parent -->
  9. <style name="my_style_2" parent="@style/my_style_1">
  10. <!-- 定义与指定View相关的若干属性 -->
  11. <item name="android:textSize">30sp</item>
  12. <item name="android:textColor">#FFFF0000</item>
  13. <item name="android:hint">load from style 2</item>
  14. </style>

  15. <!-- 定义my_style_3,用android的EditText作为parent -->
  16. <style name="my_style_3" parent="@android:style/Widget.EditText">
  17. <!-- 定义与指定View相关的若干属性 -->
  18. <item name="android:hint">"load from style 3"</item>
  19. <item name="android:textStyle">bold|italic</item>
  20. <item name="android:typeface">monospace</item>>
  21. <item name="android:background">@drawable/mybackground</item>
  22. </style>

  23. <!-- 定义MyTheme,用android的Theme作为parent -->
  24. <style name="MyTheme" parent="@android:style/Theme">
  25. <item name="android:textSize">20sp</item>
  26. <item name="android:textColor">#FF0000FF</item>
  27. <item name="android:hint">"load from style 3"</item>
  28. <item name="android:textStyle">bold|italic</item>
  29. <item name="android:typeface">monospace</item>>
  30. <item name="android:background">@drawable/gallery_selected_pressed</item>
  31. <item name="myStyle">@style/my_style_3</item>
  32. </style>
  33. </resources>
复制代码


主题和样式的定义方法类似,都是在<style>下添加N个<item>。
<style>下有两个有用的属性:
name - 以后引用时会用到;
parent - 可选,一些在自定义的style中没有指定的属性会继承parent style中的值。parent可以是android预定义的resource,也可以是自己定义的style。
<item>下定义需要改变的属性值。Android中能使用的属性可以在<sdk>/docs/reference/android/R.styleable.html中查到;也可以用自己定义的属性值;

2.使用主题
a)从AndroidManifest中指定,可以选择Application应用级:

  1. <application android:theme="@style/MyTheme">
复制代码

或是Activity级:

  1. <activity android:theme="@style/MyTheme">
复制代码


b)从Java代码中指定:

  1. public void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setTheme(R.style.MyTheme);
  4. setContentView(R.layout.main);
  5. }

复制代码

注意:setTheme必须在setContentView(),addContentView()或inflate()等实例化View的函数之前调用!

3.使用样式
a)从layout的xml文件中指定:

  1. <EditText android:id="@+id/EditText03"
  2. style="@style/my_style_3"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content">
  5. </EditText>
复制代码


b)从Java代码中指定:

  1. public void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);

  3. setTheme(R.style.MyTheme);
  4. setContentView(R.layout.main);
  5. LinearLayout ll = (LinearLayout)findViewById(R.id.llMain);

  6. EditText et = new EditText(this, null, R.attr.myStyle);
  7. ll.addView(et);
  8. }
复制代码


-----------------
附件中是用到的具体例子
referenece:<android-sdk-windows-1.5_r2>/docs/guide/topics/ui/themes.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值