成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Style在Android中的繼承關系

移動開發 Android
Android的Styles(樣式)和Themes(主題)非常類似Web開發里的CSS,方便開發者將頁面內容和布局呈現分開。Style和Theme在Android里的定義方式是完全一樣的,兩者只是概念上的區別:Style作用在單個視圖或控件上,而Theme用于Activity或整個應用程序。由于作用范圍的不同,Theme也就需要比Style包含更多的定義屬性值的項目(item)。

[[182651]]

Android的Styles(樣式)和Themes(主題)非常類似Web開發里的CSS,方便開發者將頁面內容和布局呈現分開。Style和Theme在Android里的定義方式是完全一樣的,兩者只是概念上的區別:Style作用在單個視圖或控件上,而Theme用于Activity或整個應用程序。由于作用范圍的不同,Theme也就需要比Style包含更多的定義屬性值的項目(item)。不過本文,我將Style和Theme都歸為Style來稱呼。

Android的Style和Web的CSS相比,有一個缺陷就是只能針對一個對象只能通過android:theme="@style/AppTheme"或style="@style/MyStyle"指定一個值。而CSS則可以通過class屬性在DOM元素上定義多個樣式來達到組合的效果。不過Style也有CSS沒有的功能,那就是繼承(Inheritance)。(當然CSS通過LESS和SASS這些工具也獲得繼承的能力。)

Style繼承簡介

根據Android Developers官方文檔的介紹,定義Style的繼承有兩種方式:一是通過parent標志父Style;

  1. <style name="GreenText" parent="@android:style/TextAppearance" 
  2.     <item name="android:textColor">#00FF00</item>  
  3. </style>  

另一種則是將父Style的名字作為前綴,然后通過“.”連接新定義Style的名字:

  1. <style name="CodeFont.Red"
  2. <item name="android:textColor">#FF0000</item> 
  3. </style>  

第二種方式可以***連接子Style來實踐多層繼承:

  1. <style name="CodeFont.Red.Big"
  2. <item name="android:textSize">30sp</item> 
  3. </style>  

相對***種,Android對第二種方式做出的限制就是Style必須是由自己定義的,或者說父Style和子Style必須是定義在同一個程序內,不能是引用第三方或系統的Style。畢竟對于系統的Style的引用是需要加上android:前綴作為命名空間。

其次在使用Style時,對于第二種方式定義的Style,必須引用其完全的名字,也就是說必須要包含完整的前綴和名字:

  1. <EditText 
  2. style="@style/CodeFont.Red.Big" 
  3. ... />  

Android對于***種定義方式并沒用限制,所以所有以第二種方式定義的Style都可以轉用***種:

  1. <style name="Big" parent="CodeFont.Red"
  2. <item name="android:textSize">30sp</item> 
  3. </style>  

只要parent中的名字對應上實際定義的Style名字即可。不過換成***種后Style的名字如果太簡潔就容易沖突了。

兩種繼承方式混合的效果

前面說到Style的兩種繼承方式的效果是一致的,那假如將兩種方式混在一起定義一個Style又會是什么樣的效果呢?下邊就用實際例子來分析一下。

首先定義一些實驗所需的自定義屬性(attr),(這樣可以減少系統屬性的干擾,因為系統總是會為它的屬性定義值,那樣可能無法分辨***的效果是來自系統還是定義的值)

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <resources> 
  4.  
  5.     <declare-styleable name="CustomStyle"
  6.  
  7.         <attr name="customColor" format="color"/> 
  8.  
  9.         <attr name="customText" format="string"/> 
  10.  
  11.         <attr name="customSize" format="dimension"/> 
  12.  
  13.     </declare-styleable> 
  14.  
  15. </resources>  

接著定義一個TextView的子類,并在其中獲取上邊自定義屬性的值并賦予TextView去呈現:

  1. import android.util.TypedValue; 
  2.  
  3. import android.widget.TextView; 
  4.  
  5. /** 
  6.  
  7. * @author Ider 
  8.  
  9. */ 
  10.  
  11. public class StyledTextView extends TextView { 
  12.  
  13.     public StyledTextView(Context context) { 
  14.  
  15.         this(context, null); 
  16.  
  17.     } 
  18.  
  19.     public StyledTextView(Context context, AttributeSet attrs) { 
  20.  
  21.         this(context, attrs, 0); 
  22.  
  23.     } 
  24.  
  25.     public StyledTextView(Context context, AttributeSet attrs, int defStyleAttr) { 
  26.  
  27.         super(context, attrs, defStyleAttr); 
  28.  
  29.         final TypedArray a = context.getTheme() 
  30.  
  31.                 .obtainStyledAttributes(attrs, R.styleable.CustomStyle, defStyleAttr, 0); 
  32.  
  33.         final CharSequence text = a.getText(R.styleable.CustomStyle_customText); 
  34.  
  35.         final int color = a.getColor(R.styleable.CustomStyle_customColor, Color.RED); 
  36.  
  37.         final float size = a.getDimensionPixelSize(R.styleable.CustomStyle_customSize, 70); 
  38.  
  39.         a.recycle(); 
  40.  
  41.         setText(text); 
  42.  
  43.         setTextColor(color); 
  44.  
  45.         setTextSize(TypedValue.COMPLEX_UNIT_PX, size); 
  46.  
  47.     } 
  48.  
  49.  

然后就是定義研究所需的Style

  1. <resources> 
  2.  
  3.     <style name="SuperStyleOne"
  4.  
  5.         <item name="customColor">@android:color/holo_orange_dark</item> 
  6.  
  7.         <item name="customText">Hello World</item> 
  8.  
  9.         <item name="customSize">30dp</item> 
  10.  
  11.     </style> 
  12.  
  13.     <style name="SuperStyleTwo"
  14.  
  15.         <item name="customText">www.iderzheng.com</item> 
  16.  
  17.     </style> 
  18.  
  19.     <style name="SuperStyleOne.SubOne"
  20.  
  21.         <item name="customColor">@android:color/holo_blue_dark</item> 
  22.  
  23.     </style> 
  24.  
  25.     <style name="SuperStyleOne.SubTwo" parent="SuperStyleTwo"
  26.  
  27.     </style> 
  28.  
  29.     <style name="SuperStyleOne.SubThree" parent="SuperStyleTwo"
  30.  
  31.         <item name="customText">blog.iderzheng.com</item> 
  32.  
  33.     </style> 
  34.  
  35. </resources>  

上邊定義的Style里,SuperStyleOne將通過添加前綴的方式作用到子Style上,而SuperStyleTwo則通過指定到parent來其作用??梢钥吹絊ubTwo和SubThree混合了兩種方式。

***在Activity的布局視圖里使用自定類并設定上不同的Style

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.  
  3.               xmlns:tools="http://schemas.android.com/tools" 
  4.  
  5.               android:orientation="vertical" 
  6.  
  7.               android:layout_width="match_parent" 
  8.  
  9.               android:layout_height="match_parent" 
  10.  
  11.               android:paddingLeft="@dimen/activity_horizontal_margin" 
  12.  
  13.               android:paddingRight="@dimen/activity_horizontal_margin" 
  14.  
  15.               android:paddingTop="@dimen/activity_vertical_margin" 
  16.  
  17.               android:paddingBottom="@dimen/activity_vertical_margin" 
  18.  
  19.               tools:context=".MainActivity"
  20.  
  21.     <com.ider.trial.styles.StyledTextView 
  22.  
  23.             style="@style/SuperStyleOne" 
  24.  
  25.             android:layout_width="wrap_content" 
  26.  
  27.             android:layout_height="wrap_content"/> 
  28.  
  29.     <com.ider.trial.styles.StyledTextView 
  30.  
  31.             style="@style/SuperStyleOne.SubOne" 
  32.  
  33.             android:layout_width="wrap_content" 
  34.  
  35.             android:layout_height="wrap_content"/> 
  36.  
  37.     <com.ider.trial.styles.StyledTextView 
  38.  
  39.             style="@style/SuperStyleOne.SubTwo" 
  40.  
  41.             android:layout_width="wrap_content" 
  42.  
  43.             android:layout_height="wrap_content"/> 
  44.  
  45.     <com.ider.trial.styles.StyledTextView 
  46.  
  47.             style="@style/SuperStyleOne.SubThree" 
  48.  
  49.             android:layout_width="wrap_content" 
  50.  
  51.             android:layout_height="wrap_content"/> 
  52.  
  53. </LinearLayout>  

運行之后得到效果如下: 

 

 

 

***個和第二個都是Style標準的使用方式,也看到它們正確地獲得了定義的屬性值,子Style也正確的繼承和覆蓋了父Style的屬性值。

對于第三個和第四個,它們呈現的顏色是代碼中使用的默認紅色(Color.RED),字體的值也是源自代碼中的使用值,所以明顯比前兩者要小。這也就是說它們并沒用繼承下SuperStyleOne中定義的字體大小和顏色。但是SuperStyleTwo中定義的內容被第三個正確的顯示了出來,也說明SubTwo成功繼承通過parent指定的父Style的內容。而第四個呈現出來內容則說明覆蓋的效果也是正確的。

在做這個試驗之前,我一直以為兩種方式會同時其作用,只是用parent指定比用前綴有高優先級。也就是說Android會先從當前Style定義中找某個屬性的值,如果沒有找到就轉到parent指定的父Style中找,還沒有則轉到前綴指定的父Style中找。但是通過上邊的結果表明:當使用parent指定父Style后,前綴方式則不在其作用,只是作為Style的名字。也就是說:Android的Style不支持多繼承。Style的繼承只能單線一層層下來。

反過來在看看系統定義的Style也更容易懂了,比如打開themes_holo.xml,會看到很多一樣的內容被”冗余”地定義在Theme.Holo和Theme.Holo.Light兩個Style下。但因為Theme.Holo.Light用parent指定了其父Style是Theme.Light,所以Theme.Holo.Light并沒有從Theme.Holo繼承任何屬性值,也因此這樣的冗余是必須的。

  1. <style name="Theme.Holo.Light" parent="Theme.Light"
  2.  
  3. ... ... ... ... 
  4.  
  5. </style>  

使用Theme.Holo.Light作為Style的名字只是為了名字更加的清晰明了。

References:

  1. Styles and Themes | Android Developers
  2. Android XML theme inheriting from two parent themes? – Stack Overflow
  3. xml – Reason why style attribute does not use the android: namespace prefix – Stack Overflow 
責任編輯:龐桂玉 來源: 安卓開發精選
相關推薦

2009-07-02 09:40:14

Hibernate的繼

2009-06-02 10:28:36

JPA繼承類Netbeans

2022-03-21 15:11:17

Java繼承初始化

2011-08-08 09:51:52

Cocoa 框架

2010-06-18 15:15:13

UML

2009-09-18 13:40:40

繼承關系

2010-01-19 18:51:17

C++類

2010-08-24 14:10:44

div style

2013-03-04 11:10:03

JavaJVM

2023-05-09 12:42:51

Java繼承多態

2009-09-25 14:12:16

Hibernate繼承

2010-08-09 14:01:22

關系法則

2012-05-30 15:03:43

ibmdw

2010-08-25 13:48:51

CSSlist-style-

2022-12-26 00:00:03

非繼承關系JDK

2025-01-13 00:00:00

MapStruct繼承關系Java

2010-09-15 14:09:01

GraphDataba

2017-01-17 17:13:20

AndroidAspectJ開發

2022-10-14 16:18:40

MobileNetAndroid端模型訓練

2011-07-15 15:47:02

JAVA
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 黄网站涩免费蜜桃网站 | 免费在线看a | 自拍偷拍中文字幕 | 一区二区免费在线 | 午夜免费福利电影 | 中文字幕在线观看一区 | 二区精品 | 狠狠干av| 欧美电影免费观看高清 | 欧美日韩在线观看一区 | 欧美激情视频一区二区三区在线播放 | 九九在线视频 | 国产精品久久久久久久久久久久 | 久色网| 国产精品一区二区久久 | av中文天堂| 国产资源在线播放 | 欧美在线视频一区二区 | av一级毛片| 久久天堂| 国产在线视频一区二区 | 99re视频在线 | 免费在线看黄 | 一区二区av | 国产区在线观看 | 天天爽天天操 | 日本久久网 | 激情综合五月 | 亚洲人精品午夜 | www.欧美 | 久久久久亚洲精品 | 亚洲第一福利网 | 亚洲国产精品一区二区三区 | 日本成人午夜影院 | 欧美国产91| 久久精品97 | jvid精品资源在线观看 | 日本精品视频 | 在线中文字幕亚洲 | 国产精品成人一区二区三区吃奶 | 亚洲综合成人网 |