Android style详解

样式的定义

Android的样式一般定义在res/values/styles.xml文件中,其中有一个根元素<resource>,而具体的每种样式定义则是通过<resource>下的子标签<style>来完成,<style>通过添加多个<item>来设置样式不同的属性。另外,样式是可以继承的,可通过<style>标签的parent属性声明要继承的样式,也可通过点前缀 (.) 继承,点前面为父样式名称,后面为子样式名称。点前缀方式只适用于自定义的样式,若要继承Android内置的样式,则只能通过parent属性声明。用个实例说明具体的用法吧,以下代码为Android 5.0系统默认的按钮样式:

<style name="Widget.Material.Button">
      <item name="background">@drawable/btn_default_material</item>       
      <item name="textAppearance">?attr/textAppearanceButton</item> 
      <item name="minHeight">48dip</item>
      <item name="minWidth">88dip</item>
      <item name="stateListAnimator">@anim/button_state_list_anim_material</item> 
      <item name="focusable">true</item> 
      <item name="clickable">true</item>
      <item name="gravity">center_vertical|center_horizontal</item>
</style>

其中,stateListAnimator指定状态改变时的动画,button_state_list_anim_material的代码如下:

<!-- res/anim/button_state_list_anim_material.xml -->
<?xml version="1.0" encoding="utf-8"?>
<selector  
      xmlns:android="http://schemas.android.com/apk/res/android"> 
      <item android:state_pressed="true" android:state_enabled="true">         
            <set> 
                  <objectAnimator 
                        android:propertyName="translationZ"     
                        android:duration="@integer/button_pressed_animation_duration" 
                        android:valueTo="@dimen/button_pressed_z_material" 
                        android:valueType="floatType" /> 
                  <objectAnimator 
                        android:propertyName="elevation" 
                        android:duration="0" 
                        android:valueTo="@dimen/button_elevation_material" 
                        android:valueType="floatType" /> 
             </set> 
      </item>
 <!-- base state -->
      <item 
        android:state_enabled="true"> 
             <set>
                  <objectAnimator 
                          android:propertyName="translationZ" 
                          android:duration="@integer/button_pressed_animation_duration" 
                          android:valueTo="0" 
                          android:startDelay="@integer/button_pressed_animation_delay" 
                          android:valueType="floatType"/> 
                   <objectAnimator 
                          android:propertyName="elevation" 
                          android:duration="0" 
                          android:valueTo="@dimen/button_elevation_material" 
                          android:valueType="floatType" />
               </set> 
        </item>
        <item>
                <set> 
                    <objectAnimator 
                          android:propertyName="translationZ" 
                          android:duration="0" 
                          android:valueTo="0"
                          android:valueType="floatType"/> 
                    <objectAnimator 
                         android:propertyName="elevation"
                         android:duration="0"
                         android:valueTo="0" 
                          android:valueType="floatType"/> 
                </set>
         </item>
</selector>

可以看到,每种状态的动画为属性动画集,属性动画的用法请参考Property Animation篇。现在我想继承Widget.Material.Button样式,改变背景和文字颜色,那么,代码如下:

<!-- res/values/styles.xml -->
<resources> 
    <style name="ButtonNormal" parent="@android:style/Widget.Material.Button" > 
       <item name="android:background">@drawable/bg_btn_selector</item> 
       <item name="android:textColor">@color/text_btn_selector</item> 
    </style>
</resources>

其中,@drawable/bg_btn_selector@color/text_btn_selector的实现请参照selector篇。有些按钮,我只想改变文字颜色,但背景想让它透明,这时就可以用点前缀的方式继承以上的样式,代码如下:

<!-- res/values/styles.xml -->
<resources> 
    <style name="ButtonNormal" parent="@android:style/Widget.Material.Button"> 
              <item name="android:background">@drawable/bg_btn_selector</item> 
              <item name="android:textColor">@color/text_btn_selector</item> 
     </style> 
     <style name="ButtonNormal.Transparent"> 
                <item name="android:background">@drawable/bg_btn_transparent</item>
                <item name="android:textColor">@color/text_btn_selector</item>
     </style>
</resources>

引用的时候只要在相应的Button里添加style就可以了,代码如下:

<Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:onClick="onAction" 
        android:text="@string/btn_action"   
        style="@style/ButtonNormal.Transparent" />

有时候,定义的样式太多,如果都放在styles.xml文件里,那这文件也太臃肿了。因此,可以将样式分类拆分成多个文件。Android系统本身也拆分为多个文件存放的,如下列表全都是样式文件:

styles.xml
styles_device_defaults.xml
styles_holo.xml
styles_leanback.xml
styles_material.xml
styles_micro.xml
themes.xml
themes_device_defaults.xml
themes_holo.xml
themes_leanback.xml
themes_material.xml
themes_micro.xml

其中,主要分为两大类,styles定义了简单的样式,而themes则定义了主题。
主题
以上的简单例子只用于单个View,这是样式最简单的用法。但样式的用法不只是用于单个View,也能用于Activity或整个Application,这时候需要在相应的<activity>标签或<application>标签里设置android:theme属性,引用的其实也是style**,但一般称为主题。
Android系统提供了多套主题,查看Android的frameworks/base/core/res/res/values目录,就会看到有以下几个文件(目前为止):

themes.xml:                      低版本的主题,目标API level一般为10或以下

themes_holo.xml:                 从API level 11添加的主题

themes_device_defaults.xml:      从API level 14添加的主题

themes_material.xml:             从API level 21添加的主题

themes_micro.xml:                应该是用于Android Wear的主题

themes_leanback.xml:             还不清楚什么用

不过在实际应用中,因为大部分都采用兼容包的,一般都会采用兼容包提供的一套主题:Theme.AppCompatAppCompat主题默认会根据不同版本的系统自动匹配相应的主题,比如在Android 5.0系统,它会继承Material主题。不过这也会导致一个问题,不同版本的系统使用不同主题,就会出现不同的体验。因此,为了统一用户体验,最好还是自定义主题。
自定义主题也很简单,只要继承某一父主题,然后在<activity>标签或<application>中引用就可以了。主题的定义示例如下:

<resources> 
      <style name="AppTheme" parent="Theme.AppCompat"> 
          <item name="windowActionBar">false</item> 
          <item name="windowNoTitle">true</item> 
          <item name="windowAnimationStyle">@style/WindowAnimation</item> 
      </style> 
      <!-- Standard animations for a full-screen window or activity. --> 
      <style name="WindowAnimation" parent="@android:style/Animation.Activity">
          <item name="activityOpenEnterAnimation">@anim/activity_open_enter</item> 
          <item name="activityOpenExitAnimation">@anim/activity_open_exit</item> 
          <item name="activityCloseEnterAnimation">@anim/activity_close_enter</item> 
          <item name="activityCloseExitAnimation">@anim/activity_close_exit</item>
      </style>
</resources>

其中,WindowAnimation重新指定了Activity的转场动画,以下为activity_close_exit的示例代码:

<?xml version="1.0" encoding="utf-8"?>
<set
      xmlns:android="http://schemas.android.com/apk/res/android"  
      android:shareInterpolator="false" 
      android:zAdjustment="top"> 
      <alpha 
              android:fromAlpha="0.0" 
              android:toAlpha="1.0"    
              android:interpolator="@interpolator/decelerate_quart" 
              android:fillEnabled="true" 
              android:fillBefore="false" 
              android:fillAfter="true" 
              android:duration="200" />
       <translate 
              android:fromYDelta="8%" 
              android:toYDelta="0" 
              android:fillEnabled="true" 
              android:fillBefore="true" 
              android:fillAfter="true"   
              android:interpolator="@interpolator/decelerate_quint" 
              android:duration="350" />
</set>

这是比较简单的视图动画,视图动画具体用法可参考View Animation篇。接着,若要使用到整个Application,则在AndroidManifest.xml<application>标签设置android:theme属性,示例代码如下:

<application 
      android:allowBackup="true" 
      android:icon="@mipmap/ic_launcher"   
      android:label="@string/app_name"   
      android:theme="@style/AppTheme"> 
      <!-- activity here -->
</application>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容