越来越多互联网企业都在Android平台上部署其客户端,为了提升用户体验,这些客户端都做得布局合理而且美观.......Android的Style设计就是提升用户体验的关键之一。Android上的Style分为了两个方面:

  1. Theme是针对窗体级别的,改变窗体样式;
  2. Style是针对窗体元素级别的,改变指定控件或者Layout的样式。

Android系统的themes.xml和style.xml(位于/base/core/res/res/values/)包含了很多系统定义好的style,建议在里面挑个合适的,然后再继承修改。

Style是View中一些属性的集合,包括height,padding,font color,background等等,Style单独定义在xml文件中,类似与web页面中css的角色,将设计和内容分开,便于修改和重复使用。

定义Style:

style文件需要保存在res/values目录下,文件名任意,但是必须是xml文件,sytle文件的根标记必须是<resources>。写了一个简单示例,效果如下:

main.xml文件代码:

[c-sharp] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"android:layout_height="fill_parent">
  4. <TextView
  5. style="@style/CodeFont"mce_style="@style/CodeFont"
  6. android:text="测试style"/>
  7. </LinearLayout>

声明style是CodeFont,对应的是style文件中的style name。mystyle.xml文件中定义了style name是CodeFont:

[c-sharp] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <mce:stylename="CodeFont"parent="@android :style/TextAppearance.Medium"><!--
  4. <itemname="android:layout_width">fill_parent</item>
  5. <itemname="android:layout_height">wrap_content</item>
  6. <itemname="android:textColor">#00FF00</item>
  7. <itemname="android:typeface">monospace</item>
  8. --></mce:style><stylename="CodeFont"parent="@android :style/TextAppearance.Medium"mce_bogus="1">
  9. <itemname="android:layout_width">fill_parent</item>
  10. <itemname="android:layout_height">wrap_content</item>
  11. <itemname="android:textColor">#00FF00</item>
  12. <itemname="android:typeface">monospace</item>
  13. </style>
  14. </resources>

parent属性表示style之间可以继承,同时可以覆盖parent style的一些属性。

style继承有两种方式:

  • style的继承可以通过parent属性,用来继承android已经定义好的style,例如: [c-sharp] view plain copy print ?
    1. <mce:stylename="GreenText"parent="@android :style/TextAppearance"><!--
    2. <itemname="android:textColor">#00FF00</item>
    3. --></mce:style><stylename="GreenText"parent="@android :style/TextAppearance"mce_bogus="1">
    4. <itemname="android:textColor">#00FF00</item></style>
  • 继承了android中的TextAppearance,同时覆盖了android:textColor属性。
  • 如果要继承自定义的style,不需要通过parent属性,只要style的name以需要继承的style的name开始后跟新的style的name,中间用“.”隔开。注意:这种方式只适用与自定义的style继承

[c-sharp] view plain copy print ?
  1. <mce:stylename="CodeFont.Red"><!--
  2. <itemname="android:textColor">#FF0000</item>
  3. --></mce:style><stylename="CodeFont.Red"mce_bogus="1">
  4. <itemname="android:textColor">#FF0000</item></style>

新的style继承了CodeFont,则在修改上边例子中的main.xml为:

[c-sharp] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"android:layout_height="fill_parent">
  4. <TextView
  5. style="@style/CodeFont.Red"mce_style="@style/CodeFont.Red"
  6. android:text="测试style"/>
  7. </LinearLayout>


效果如下,字体颜色变为了红色:

style可以多级继承:

[c-sharp] view plain copy print ?
  1. <mce:stylename="CodeFont.Red.Big"><!--
  2. <itemname="android:textSize">30sp</item>
  3. --></mce:style><stylename="CodeFont.Red.Big"mce_bogus="1">
  4. <itemname="android:textSize">30sp</item></style>

字号变大,效果如下:


sytle的更多属性见android包下的R.attr。需要注意,并不是所有的View都支持定义的style的属性,如果自定义的sytle中包含View不支持的属性,程序会自动忽略它。

Theme:

如果声明一个style作为Theme,需要配置mainfest文件中<activity> 或 <application>的android:theme 属性。

将自定义的style作为application的theme:

修改mystyle.xml为:

[c-sharp] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <mce:stylename="CodeFont"><!--
  4. <itemname="android:textSize">20sp</item>
  5. <itemname="android:typeface">monospace</item>
  6. --></mce:style><stylename="CodeFont"mce_bogus="1">
  7. <itemname="android:textSize">20sp</item>
  8. <itemname="android:typeface">monospace</item>
  9. </style>
  10. </resources>

在mainfest 的application中添加 android:theme属性:

[c-sharp] view plain copy print ?
  1. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name"
  2. android:theme="@style/CodeFont">

则application中的所有text字体都会改变,效果如下:


在每个<activity>标签中使用android:theme属性:

[c-sharp] view plain copy print ?
  1. <activityandroid:name=".MainActivity"
  2. android:label="@string/app_name"android:theme="@style/CodeFont">

android:theme还可以配置android中已经存在的theme:

[c-sharp] view plain copy print ?
  1. <activityandroid:theme="@android :style/Theme.Translucent">

如果想调整android已经定义好的theme,则可以通过自定义style来实现,例如:

[c-sharp] view plain copy print ?
  1. <colorname="custom_theme_color">#b0b0ff</color>
  2. <mce:stylename="CustomTheme"parent="android:Theme.Light"><!--
  3. <itemname="android:windowBackground">@color/custom_theme_color</item>
  4. <itemname="android:colorBackground">@color/custom_theme_color</item>
  5. --></mce:style><stylename="CustomTheme"parent="android:Theme.Light"mce_bogus="1">
  6. <itemname="android:windowBackground">@color/custom_theme_color</item>
  7. <itemname="android:colorBackground">@color/custom_theme_color</item></style>

效果如下:


level为11以下的theme:

1、Theme:

它的意思为默认状态,即如果theme这里不填任何属性的时候,默认为Theme。

api原文为:

The default system theme. This is the theme used for activities that have not explicitly set their own theme.

You can count on this being a dark background with light text on top, but should try to make no other assumptions about its appearance. In particular, the text inside of widgets using this theme may be completely different, with the widget container being a light color and the text on top of it a dark color.

效果图如下:

1.1Theme_NoDisplay

它的意思为任何都不显示。比较适用于只是运行了activity,但未显示任何东西。

api原文如下:

Default theme for activities that don’t actually display a UI; that is, they finish themselves before being resumed.

效果图如下:

1.2、Theme_NoTitleBar

意思为:背景主题的没有标题栏的样式,默认如果没有设置的话,显示黑背景

api原文:

Variant of the default (dark) theme with no title bar

效果图如下:

1.3、Theme_NoTitleBar_Fullscreen

意思为:背景主题的没有标题栏且全屏的样式,默认为黑背景

api原文:

Variant of the default (dark) theme that has no title bar and fills the entire screen

效果图如下:

2、Theme_Black:

它的意思为默认状态下黑背景。

api原文如下:

Special variation on the default theme that ensures the background is completely black. This is useful for things like image viewers and media players. If you want the normal (dark background) theme donotuse this, useTheme.

效果图如下:

2.1、Theme_Black_NoTitleBar:

意思为:黑背景主题的没有标题栏的样式

api原文:

Variant of the black theme with no title bar

效果图如下:

2.2、Theme_Black_NoTitleBar_Fullscreen

意思为:黑背景主题的没有标题栏且全屏的样式

api原文:

Variant of the black theme that has no title bar and fills the entire screen

效果图如下:

3、Theme_Light

意思为:默认状态下亮背景,与上述黑背景Theme_Black相反。

api原文:

Theme for a light background with dark text on top. Set your activity to this theme if you would like such an appearance. As with the default theme, you should try to assume little more than that the background will be a light color.

效果图如下:

3.1、Theme_Light_NoTitleBar

意思为:亮背景主题的没有标题栏的样式,与Theme_Black_NoTitleBar相反

api原文:

Variant of the light theme with no title bar

效果图如下:

3.2、Theme_Light_NoTitleBar_Fullscreen

意思为:亮背景主题的没有标题栏且全屏显示的样式,与Theme_Black_NoTitleBa_Fullscreenr相反

api原文:

Variant of the light theme that has no title bar and fills the entire screen

效果图如下:

4、Theme_Dialog

意思为:对话框样式 将整个activity变成对话框样式出现。

api原文:

Default theme for dialog windows and activities, which is used by theDialogclass. This changes the window to be floating (not fill the entire screen), and puts a frame around its contents. You can set this theme on an activity if you would like to make an activity that looks like a Dialog.

效果图如下:这里需要自定义大小,否则显示不全。

5、Theme_InputMethod

6、Theme_Panel

意思为:删除掉所有多余的窗口装饰,在一个空的矩形框中填充内容,作用范围相当于把dialog中的所有元素全部去掉,只是一个空的矩形框,且此为默认的样式。

api原文:

Default dark theme for panel windows. This removes all extraneous window decorations, so you basically have an empty rectangle in which to place your content. It makes the window floating, with a transparent background, and turns off dimming behind the window.

效果图如下:这里需要自定义大小,否则显示不全。

6.1、Theme_Light_Panel

意思为:删除掉所有多余的窗口装饰,在一个空的矩形框中填充内容,作用范围相当于把dialog中的所有元素全部去掉,只是一个空的矩形框,且默认是light的样式。

api原文:

Default light theme for panel windows. This removes all extraneous window decorations, so you basically have an empty rectangle in which to place your content. It makes the window floating, with a transparent background, and turns off dimming behind the window.

效果图如下:这里需要自定义大小,否则显示不全。

7、

Theme_Wallpaper

意思为:使用墙纸做主题,默认状态。

api原文:

Default theme for windows that want to have the user’s selected wallpaper appear behind them.

效果图如下:

7.1、Theme_WallpaperSettings

意思为:使用墙纸做主题,默认是使用将上一个界面调暗之后作为主题,(这里我很疑惑为什么是上一个界面变暗而不是墙纸主题变暗呢?)

api原文:

Theme for a wallpaper’s setting activity that is designed to be on top of a dark background.

效果图如下:

7.2、Theme_Light_WallpaperSettings

意思为:使用墙纸做主题,默认Light状态。

api原文:

Theme for a wallpaper’s setting activity that is designed to be on top of a light background.

效果图如下:

7.3、Theme_Wallpaper_NoTitleBar

意思为:使用墙纸做主题,且没有标题栏

api原文:

Variant of the translucent theme with no title bar

效果图如下:

7.4、Theme_Wallpaper_NoTitleBar_Fullscreen

意思为:使用墙纸做主题,且没有标题栏,且全屏显示

api原文:

Variant of the translucent theme that has no title bar and fills the entire screen

效果图如下:

8、Theme_Translucent

意思为:半透明状态下的背景,将运行此activity之前的屏幕作为半透明状态作为此activity运行时的样式。

api原文:

Default theme for translucent activities, that is windows that allow you to see through them to the windows behind. This sets up the translucent flag and appropriate animations for your windows.

效果图如下:

8.1、Theme_Translucent_NoTitleBar

意思为:半透明状态下没有标题栏的背景,将运行此activity之前的屏幕作为半透明状态作为此activity运行时的样式。

api原文:

Variant of the translucent theme with no title bar

效果图如下:

8.2、Theme_Translucent_NoTitleBar_Fullscreen

意思为:半透明状态下没有标题栏且全屏的背景,将运行此activity之前的屏幕作为半透明状态作为此activity运行时的样式。

api原文:

Variant of the translucent theme that has no title bar and fills the entire screen

效果图如下:

更多相关文章

  1. android EditText设置不可写
  2. TabHost与RadioGroup结合完成的菜单【带效果图】5个Activity
  3. Android(安卓)Activity的启动
  4. APP开发实战94-Vector静态图的使用
  5. Android(安卓)开发中的倒计时
  6. Gradle多渠道打包
  7. [APP] Android(安卓)开发笔记 006-使用短信验证SDK进行短信验证
  8. Android(安卓)5.x特性概览二
  9. android listview custom style 自定义样式

随机推荐

  1. 记录app端嵌入式H5页面
  2. 备忘录
  3. 开源项目Universal Image Loader for And
  4. android判断模拟器的三种方法
  5. android的内存监控
  6. android的第一个程序helloworld (有图有真
  7. Android执行文件apk的组成结构
  8. android 根据滑动隐藏或显示导航 类似手
  9. android studio打开旧项目遇到build.grad
  10. Android内核与主线linux内核的比较(Androi