Android Styles / Themes要点整理

 (整理得比较草,用于以后查找)

 
style作用于View,而theme是作用于activity或application的style。

一共如下几个要点:
     Style的存放位置
     所有可能可能的style属性
     一套Theme里面可以包括的style属性
     Android系统本身已经提供的大量的theme
     系统缺省的theme和IDE开发环境自动配置的theme
     style和theme的继承和修正


Style的存放位置

存放在在res/values目录里面

To create a set of styles, save an XML file in the  res/values/  directory of your project. The name of the XML file is arbitrary, but it must use the  .xml  extension and be saved in the  res/values/  folder.



所有可能可能的style属性:

The best place to find properties that apply to a specific View is the corresponding class reference, which lists all of the supported XML attributes. For example, all of the attributes listed in the table of TextView XML attributescan be used in a style definition for a TextView element (or one of its subclasses). One of the attributes listed in the reference is android:inputType, so where you might normally place the android:inputType attribute in an<EditText> element, like this:

<EditText
    android:inputType="number"
    ... />

找某个view的可能style:
查找该类的 XML attributes属性

如果找所有的: For a reference of all available style properties, see the  R.attr  reference.
也即这里: http://developer.android.com/reference/android/R.attr.html



一套Theme里面可以包括的style属性

A list of the standard attributes that you can use in themes can be found at  R.styleable.Theme .



Android系统本身已经提供的大量的theme

The Android platform provides a large collection of styles and themes that you can use in your applications. You can find a reference of all available styles in the  R.style  class.
int Theme_Holo_Dialog_NoActionBar_MinWidth Variant of Theme.Holo.Dialog.NoActionBar that has a nice minimum width for a regular dialog.
注意上面左边绿色字体是我们代码中的写法,右边蓝色是其实际的值。

系统已经提供了大量Theme
  1. R.style for Android styles and themes

但又说文档并不全面,因为没有给出细节,如果要看细节的话,看下面两个style和theme的源代码链接:

The R.style reference, however, is not well documented and does not thoroughly describe the styles, so viewing the actual source code for these styles and themes will give you a better understanding of what style properties each one provides. For a better reference to the Android styles and themes, see the following source code:

These files will help you learn through example. For instance, in the Android themes source code, you'll find a declaration for <style name="Theme.Dialog">. In this definition, you'll see all of the properties that are used to style dialogs that are used by the Android framework.



系统缺省的theme和IDE开发环境自动配置的theme

看到这样一段话: Beginning with Android 3.0 (API level 11), the action bar is included in all activities that use the  Theme.Holo  theme (or one of its descendants), which is the default theme when either the targetSdkVersion  or  minSdkVersion  attribute is set to  "11"  or greater. For example:

也即,在API level 11之后,就有缺省的theme了,而且这个缺省的"Theme.Holo"里面还包括了ActionBar等。

经过多次建新项目的尝试,发现有两类自动的style添加:
1、Android系统自行添加的theme,跟版本有关,如上所提及。注意,这些自行添加的东西在项目代码或配置文件中没有找到,可能是在android系统自己的framework里面自行添加的,故不能删除,只能通过程序代码将其关掉。
2、IDE开发环境在创建项目时,可以通过项目配置来添加theme。注意这个添加的theme在项目代码或配置文件中可以看见,故可以直接代码修改。


style和theme的继承和修正

1、系统提供的style可以继承并修正:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
        <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:typeface">monospace</item>
    </style>
</resources>
可见继承使用了 parent标示。注意系统内部也一样使用“.”来继承,可能这种方法不能跨越系统和自定义。

2、自定义的也可以继承和修正,但是格式不同,使用“.”来表示继承:
<style name="CodeFont.Red">
        <item name="android:textColor">#FF0000</item>
    </style>

3、应用style到view:
<TextView
    style="@style/CodeFont"
    android:text="@string/hello" />

4、应用theme到application或activity:
<application android:theme="@style/CustomTheme">
<activity android:theme="@android:style/Theme.Dialog">

5、目录名和平台版本(API Level)对应,平台版本升级后自动对应自己的目录:

For example, here is the declaration for a custom theme which is simply the standard platforms default light theme. It would go in an XML file under res/values (typically res/values/styles.xml):

<style name="LightThemeSelector" parent="android:Theme.Light">
    ...
</style>

To have this theme use the newer holographic theme when the application is running on Android 3.0 (API Level 11) or higher, you can place an alternative declaration for the theme in an XML file in res/values-v11, but make the parent theme the holographic theme:

<style name="LightThemeSelector" parent="android:Theme.Holo.Light">
    ...
</style>

Now use this theme like you would any other, and your application will automatically switch to the holographic theme if running on Android 3.0 or higher.

但是感觉这个用目录的方法不太好,不太规范,还是用变量之类较好。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值