首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

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

2012-09-02 
自定义样式(style)与主题(theme)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应用级:?
? ? <application android:theme="@style/MyTheme">
或是Activity级:? ???<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. ? ? }
复制代码

?

转载:http://www.cnblogs.com/wangtianxj/archive/2010/02/21/1670668.html

?

热点排行