1.sytle:Android中的style就是用简单的配置来实现页面的外观和风格的方式。他是一个包含一个或者多个view控件属性的集合,可以当成一个整体应用到XML单个元素上。

例如我们可以在res/values/styles.xml文件,键入代码:

Xml代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stylename="style1"><!--为样式定义一个全局唯一的名字-->
  4. <itemname="android:textSize">18px</item><!--name属性为样式要用在的View控件持有的属性-->
  5. <itemname="android:textColor">#0000CC</item>
  6. <!--
  7. 等等……
  8. -->
  9. <itemname="android:layout_width">60dip</item>
  10. <itemname="android:layout_height">50dip</item>
  11. <itemname="android:layout_weight">1</item>
  12. <itemname="android:divider">#FFCFCFCF</item>
  13. <itemname="android:dividerHeight">0.5dip</item>
  14. <itemname="android:listSelector">@drawable/list_item_bg</item>
  15. <itemname="android:cacheColorHint">#00000000</item>
  16. </style>
  17. <stylename="TitleStyle">
  18. <itemname="android:textSize">18sp</item>
  19. <itemname="android:textColor">#ec9237</item>
  20. </style>
  21. <stylename="LinkStyle">
  22. <itemname="android:textSize">18sp</item>
  23. <itemname="android:textColor">#ec0032</item>
  24. <itemname="android:fromAlpha">0.0</item>
  25. <itemname="android:toAlpha">0.0</item>
  26. </style>
  27. <stylename="SpecialText">
  28. <itemname="android:textSize">28sp</item>
  29. <itemname="android:textColor">@color/darkgreen</item>
  30. <itemname="android:gravity">center</item>
  31. <itemname="android:textStyle">bold|italic</item>
  32. <itemname="android:background">@drawable/icon</item>
  33. </style>
  34. </resources>

这其实也就是把view的属性罗列出来,用一个view关联。这样在遇到view配置相同的属性的时候就可以直接通过这个名字直接关联这样的样式设置了。其实这个还想是WEB开发中的CSS的使用。就是把view的属性单独写出,提高重用性。

在layout文件中可以像下面这样使用上面的android样式:

Xml代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextViewstyle="@style/TitleStyle"
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:gravity="center_vertical|center_horizontal"
  10. android:text="txlong_onzaislna"/>
  11. <TextViewstyle="@style/LinkStyle"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:gravity="center_vertical|center_horizontal"
  15. android:text="http://txlong-onz.iteye.com"
  16. android:autoLink="all"/>
  17. <TextViewstyle="@style/SpecialText"
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:gravity="center_vertical|center_horizontal"
  21. android:text="SpecialText"/>
  22. </LinearLayout>

<style>标签中有一个parent属性。这个属性可以让当前样式继承一个父样式,当前样式可以继承到父样式的值。当然,如果父样式的值不符合你的需求,你也可以对它进行修改,和CSS中的覆盖效果一样,都是以最后的为准,例子如下:

Xml代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stylename="style1">
  4. <itemname="android:textSize">18px</item><!--name属性为样式要用在的View控件持有的属性-->
  5. <itemname="android:textColor">#0000CC</item>
  6. </style>
  7. <stylename="subitcast"parent="@style/style1">
  8. <itemname="android:textColor">#FF0000</item>
  9. </style>
  10. </resources>

样式的继承,一种是继承平台自带的。一种是继承自定义的样式。继承平台原有的一定要parent指定。

Xml代码
  1. <stylename="GreenText"parent="@android:style/TextAppearance">
  2. <itemname="android:textColor">#00FF00</item>
  3. </style>

继承自定义的只需在name前加要继承的style主题就行了。<style name="GreenText.Red">

Xml代码
  1. <itemname="android:textColor">#FF0000</item>
  2. lt;/style>

这种方法可以不断地继承

Xml代码
  1. <stylename="CodeFont.Red.Big">
  2. <itemname="android:textSize">30sp</item>
  3. </style>

2.theme:Android主题设置其实就是将主题添加到一个配置里,以方便调用,提高重用性。所以配置文件的属性也就是窗口等的主题样式。

Xml代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stylename="theme1">
  4. <itemname="android:windowNoTitle">true</item>
  5. <itemname="android:windowFullscreen">?android:windowNoTitle</item>
  6. </style>
  7. </resources>

上面"?android:windowNoTitle"中的问号用于引用在当前主题中定义过的资源的值。下面代码显示在AndroidManifest.xml中如何为应用设置上面定义的主题:

Xml代码
  1. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name"
  2. android:theme="@style/theme1">
  3. <activityandroid:name=".MessageShowActivity"android:label="@string/app_name"
  4. android:windowSoftInputMode="adjustPan"android:screenOrientation="portrait"
  5. android:theme="@style/theme2">
  6. </activity>
  7. </application>

除了可以在AndroidManifest.xml中设置主题,同样也可以在代码中设置主题,如下:

setTheme(R.style.theme1);

尽管在定义上,样式和主题基本相同,但是它们使用的地方不同。样式用在单独的View,如:EditText、TextView等;主题通过AndroidManifest.xml中的<application>和<activity>用在整个应用或者某个 Activity,主题对整个应用或某个Activity存在全局性影响。如果一个应用使用了主题,同时应用下的view也使用了样式,那么当主题与样式属性发生冲突时,样式的优先级高于主题。

另外android系统也定义了一些主题,例如:

<activity android:theme="@android:style/Theme.Dialog">,该主题可以让Activity看起来像一个对话框,

<activity android:theme="@android:style/Theme.Black.NoTitleBar">Variant of the light theme with no title bar,系统自带的黑色主题。如果需要查阅这些主题,可以在文档的reference-->android-->R.style 中查看。

3.AlertDialog的使用Demo:



主要的类在这里粘出来,style和theme自己下载下边的附件。

Java代码
  1. publicclassStyleThemeTestextendsActivity{
  2. publicfinalStringTAG="Test";
  3. privateDialogmDialog;
  4. privateButtonbutton1;
  5. privateButtonbutton2;
  6. privateButtonbutton3;
  7. @Override
  8. publicvoidonCreate(BundlesavedInstanceState){
  9. StyleThemeTest.this.setTheme(R.style.theme1);
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.main);
  12. button1=(Button)findViewById(R.id.button1);
  13. button2=(Button)findViewById(R.id.button2);
  14. button3=(Button)findViewById(R.id.button3);
  15. button1.setOnClickListener(newView.OnClickListener(){
  16. @Override
  17. publicvoidonClick(Viewv){
  18. mDialog=newAlertDialog.Builder(StyleThemeTest.this)
  19. .setIcon(android.R.drawable.ic_dialog_alert)
  20. .setTitle("Warning!")
  21. .setMessage("AreyousuretoFolloworUnFollowthefriend?")
  22. .setPositiveButton("Ok",newDialogInterface.OnClickListener(){
  23. publicvoidonClick(DialogInterfacedialog,intwhichButton){
  24. Log.v(TAG,"OK");
  25. }
  26. })
  27. .setNeutralButton("Cancel",newDialogInterface.OnClickListener(){
  28. publicvoidonClick(DialogInterfacedialog,intwhichButton){
  29. Log.v(TAG,"Cancale");
  30. }
  31. }).create();
  32. mDialog.show();//如果要显示对话框,一定要加上这句
  33. }
  34. });
  35. button2.setOnClickListener(newView.OnClickListener(){
  36. @Override
  37. publicvoidonClick(Viewv){
  38. Builderbuilder=newAlertDialog.Builder(StyleThemeTest.this);
  39. builder.setIcon(android.R.drawable.alert_dark_frame);
  40. builder.setTitle("AlertDialogTitle.");
  41. builder.setMessage("Whatdothinkaboutthis?");
  42. builder.setNegativeButton("好",newDialogInterface.OnClickListener(){
  43. @Override
  44. publicvoidonClick(DialogInterfacedialog,intwhich){
  45. Log.v(TAG,"good");
  46. }
  47. });
  48. builder.setNeutralButton("中立",newDialogInterface.OnClickListener(){
  49. @Override
  50. publicvoidonClick(DialogInterfacedialog,intwhich){
  51. Log.v(TAG,"justsoso");
  52. }
  53. });
  54. builder.setPositiveButton("不好",newDialogInterface.OnClickListener(){
  55. @Override
  56. publicvoidonClick(DialogInterfacedialog,intwhich){
  57. Log.v(TAG,"bad");
  58. }
  59. });
  60. builder.create().show();
  61. }
  62. });
  63. button3.setOnClickListener(newView.OnClickListener(){
  64. @Override
  65. publicvoidonClick(Viewv){
  66. newAlertDialog.Builder(StyleThemeTest.this)
  67. .setMessage("Thereisaxxxxxxxxxxxxerrorhanppened!!!")
  68. .setPositiveButton("知道了!",newDialogInterface.OnClickListener(){
  69. publicvoidonClick(DialogInterfacedialog,intwhichButton){
  70. Log.v(TAG,"IKNOW.");
  71. }
  72. }).create().show();
  73. }
  74. });
  75. }
  76. }

原文出自:http://txlong-onz.iteye.com/blog/930222

更多相关文章

  1. 21.Android系统属性build.prop文件(笔记)
  2. select在ios和android中样式不兼容
  3. TabLayout属性详解
  4. android 系统属性
  5. 【起航计划 014】2015 起航计划 Android(安卓)APIDemo的魔鬼步伐
  6. 布局及视图(一)
  7. android之Android(安卓)Studio下自定义属性的定义和使用
  8. Android(安卓)Navigation使用
  9. Android(安卓)Flexboxlayout使用详解

随机推荐

  1. Android应用程序内存分析-Memory Analysi
  2. -如何将Eclipse中的项目迁移到Android(安
  3. 在eclipse中查看android SDK的源代码
  4. 高焕堂
  5. Android(安卓)Camera
  6. EditText的使用
  7. Android(安卓)输入法键盘和activity页面
  8. 让android支持RTSP(live555分析)
  9. Android(安卓)Animation动画 控制动画的
  10. Android图形合成和显示系统---基于高通MS