android style和attr的用法

一  、对style和attr的引用

1. 当引用平台的style做为style的parent时,“@android:style/主题” 
== “@android:主题” ==“   android:style/主题 ”== “android:主题”;
2.
当引用平台的style作为属性的引用时,“@android:style/主题”;
3.
当引用自定义style作为其他style的parent时,“@style/主题” == “style/主题” == “主题”;
4.
当引用自定义style作为其他属性的引用时,“@style/主题”;
5. 当引用平台属性作为属性的引用时,“?android:attr/属性” ==
“?android:属性”;
6. 当引用自定义属性时,“?attr/属性” ==
“?属性”;

7. 上述六个情况中,可以在‘@‘或‘?‘后加入‘*‘以引用被隐藏(即平台私有)的资源;

8.
如果引用平台资源或属性时,可以将“android:”放在斜杠“/”的后面,即,@android:style/主题”==
@style/android:主题”,“?android:attr/属性”== “?attr/android:属性”;

二、 如何自定义属性

1:

<resources>

        <attr
name="anr">
           
<enum name="none" value="0" />
     
      <enum name="thumbnail" value="1"
/>
            <enum
name="drop" value="2" />
       
</attr>
   
</resources>

这种方式定义属性后,其他的<declare-styleable>块内可以直接使用,但不能带有任何名字或格式说明,e.g:

<declare-styleable
name="TextView">

<attr
name="anr">

</declare-styleable>

下面是错误的:

<declare-styleable
name="TextView">
         
<attr name="anr"  format="string">

</declare-styleable>

因为这相当于重新定义了"anr",这是不允许的。因此,<declare-styleable>块外出现的属性优先于<declare-styleable>块内出现的属性,即,如果<declare-styleable>块内出现某一带有format描述的属性,而<declare-styleable>块外也有一个同名属性(没有format说明),则是错误的。

此外,如果对首次出现在<declare-styleable>内的属性指定了format,则其他的<declare-styleable>内可直接使用这个属性,但不能再指定其他format。这种情况类似于使用首次出现<declare-styleable>块外的属性。

因此,在<declare-styleable>块外的属性最好都指明format说明,以便为<declare-styleable>块内使用。

e.g:

如果,

<resources>
        <attr
name="anr" />

</resources>

然后,

或者

<declare-styleable
name="TextView">
         
<attr name="anr">
         
  <enum name="none" value="0" />
     
      <enum name="thumbnail" value="1"
/>
            <enum
name="drop" value="2" />
        
</attr>

</declare-styleable>

这些都是错误的!

2:

<declare-styleable
name="Theme">
        <attr
name="colorText" 
format="color|reference"/>
       
<attr name="colorForeground"
format="color"/>
        <attr
name="colorForegroundInverse" format="color"/>
  
</declare-styleable>

这种方式定义属性的前提是此前没有对colorText,
colorForeground,colorForegroundInverse的任何定义。同第一种一样,以后的<declare-styleable>块内可以直接使用,但不能再做其他改动,e.g:

<declare-styleable
name="TextView">
         
<attr name="colorText">

</declare-styleable>

或者:

<declare-styleable
name="TextView">
         
<attr
name="colorText">
         
<attr name="colorForeground">

</declare-styleable>

但下面是错误的:

<declare-styleable
name="TextView">
         
<!--不能再有format="color|reference"说明-->
         
<attr name="colorText"
format="color|reference">
         
<attr name="colorForeground">
  
</declare-styleable>

三、 如何使用平台自带的属性

e.g:

<declare-styleable
name="FragmentArguments">
       
<!--不能有格式说明-->
        <attr
name="android:label" />

</declare-styleable>

此时,在R.styleable内部会有个属性的定义,名为FragmentArguments_android_label。
 
但是,不能对平台自带的属性重新定义,e.g:

<resources>
        <attr
name="android:label" />

</resources>

但可以这样:

<resources>
        <attr
name="label" format="string"/>

</resources>

 如何继承style

可以使用点‘.’和“parent”来继承自定义的style,而要想继承平台style,则只能使用“parent”。

四、 如何继承style

可以使用点‘.’和“parent”来继承自定义的style,而要想继承平台style,则只能使用“parent”。

e.g:

1.
    <style name="HelloTheme"
parent="@android:style/Theme.Light"
>
          <item
name="colorForeground">#770000</item>
         
<item
name="colorBackground">#000000</item>
         
<item
name="colorText">#00ff00</item>
         
<item name="android:windowBackground" >@drawable/windowoverlay
</item>
          <item
name="android:windowNoTitle">false</item>

</style>

2.
     <style
name="HelloTheme" 
parent="HelloTheme.MyHelloTheme">
       
<item name="windowBackground" >@drawable/overlaybk
</item>
        <item
name="colorText">#00ff00</item>
   
</style>
    <style name="HelloTheme.Hello"
>
        <!--<item
name="android:background">?attr/windowBackground</item>-->
       
<!--<item
name="android:textColor">?attr/colorText</item>-->
       
<item
name="android:background">?windowBackground</item>
       
<item
name="android:textColor">?colorText</item>

</style>

3.
   <style
name="Holo">
        <item
name="textViewStyle">@style/HelloTheme.Hello</item>

</style>

4.
   <style
name="Holo.TextViewStyle" />

如果对一个TextView使用Holo.TextViewStyle时,不会起到任何作用。应该直接使用:

  
<style
name="TextViewStyle">
       
<item
name="android:background">?windowBackground</item>
       
<item
name="android:textColor">?colorText</item>
  
</style>

或者

   <style
name="TextViewStyle_1"  parent="TextViewStyle" >
 
 
或者
 
  
<style name="TextViewStyle.TextViewStyle_1">

由此可知,属性引用(reference)可以传递,当然前提是应用或活动的主题(theme)中使用了windowBackground和colorText,上例中:

   
<item
name="android:background">?windowBackground</item>
    
<item
name="android:textColor">?colorText</item>

等价于:

   
<item
name="android:background">@drawable/overlaybk</item>
    
<item
name="android:textColor">#00ff00</item>

因为,windowBackground和colorText作为两个属性的引用,在这里已被设置:

   
<style name="HelloTheme" 
parent="HelloTheme.MyHelloTheme">
       
<item name="windowBackground" >@drawable/overlaybk
</item>
       
<item
name="colorText">#00ff00</item>
   
</style>
 
 自定义属性时,在parent处指定的继承平台已有属性时偶尔会提示资源不存在

e.g:

parent="@android:style/TextAppearance.Holo.Light.Inverse"

error: Error retrieving parent for item: No resource
found that matches the given name
‘@android:style/TextAppearance.Holo.Light.Inverser‘.

这种写法是错误的,虽然平台下的data/res/styles.xml内有该属性的定义,但是平台的android.R.style类内并不存在TextAppearance_Holo_Light_Inverse,因为此类属性是平台的私有属性,不公开的。所以,也不能使用android.R.style.TextAppearance_Holo_Light_Inverse.

若要避免错误,可以这样书写:parent="@*android:style/TextAppearance.Holo.Light.Inverse"
,或去掉‘@‘和(或)‘style/‘。

最后,如果style的名字内既出现了点‘.’,也使用“parent”,则该style只继承了parent指向的style,style的
"name"里的‘.’不会起作用。如果在style的"name"内出现了‘.’,而又没有使用"parent",则name里的点‘.‘之前的名字必须存在定义。而如果使用了parent,name内点‘.‘之前的任何名字可以不存在定义。

五、 “android:”前出现‘@’和‘?’的区别

 在定义style时经常会遇到此类情况,例如:

 在定义style时经常会遇到此类情况,例如:

<style name="Theme.IconMenu"
parent="Theme.Holo">
        <!--
Menu/item attributes -->
       
<item
name="android:itemTextAppearance">@android:style/TextAppearance.Widget.IconMenu.Item</item>
       
<item
name="android:itemBackground">?android:attr/selectableItemBackground</item>
       
<item
name="android:itemIconDisabledAlpha">?android:attr/disabledAlpha</item>
       
<item
name="android:horizontalDivider">@android:drawable/divider_horizontal_dark</item>
       
<item
name="android:verticalDivider">@android:drawable/divider_vertical_dark</item>
       
<item
name="android:windowAnimationStyle">@android:style/Animation.OptionsPanel</item>
       
<item
name="android:moreIcon">@android:drawable/ic_menu_more</item>
       
<item name="android:background">@null</item>
   
</style>

其中,<item
name="android:itemTextAppearance">@android:style/TextAppearance.Widget.IconMenu.Item</item>表明"android:itemTextAppearance"为一个style类型,它引用了平台的另一个style(TextAppearance.Widget.IconMenu.Item)。而 <item
name="android:itemIconDisabledAlpha">?android:attr/disabledAlpha</item>则表明"android:itemIconDisabledAlpha"的属性值引用当前主题下的disabledAlpha。

时间: 2024-08-29 02:19:35

android style和attr的用法的相关文章

Android Drawable 那些不为人知的高效用法

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43752383,本文出自:[张鸿洋的博客] 1.概述 Drawable在我们平时的开发中,基本都会用到,而且给大家非常的有用.那么什么是Drawable呢?能够在canvas上绘制的一个玩意,而且相比于View,并不需要去考虑measure.layout,仅仅只要去考虑如何draw(canavs).当然了,对于Drawable传统的用法,大家肯定不陌生 ,今天主要给大家带来以下几

Android中AsyncTask的简单用法【转】

在开发Android移动客户端的时候往往要使用多线程来进行操作,我们通常会将耗时的操作放在单独的线程执行,避免其占用主线程而给用户带来不好的用户体验.但是在子线程中无法去操作主线程(UI 线程),在子线程中操作UI线程会出现错误.因此android提供了一个类Handler来在子线程中来更新UI线程,用发消息的机制更新UI界面,呈现给用户.这样就解决了子线程更新UI的问题.但是费时的任务操作总会启动一些匿名的子线程,太多的子线程给系统带来巨大的负担,随之带来一些性能问题.因此android提供了

jQuery中attr()方法用法实例

本文实例讲述了jQuery中attr()方法用法.分享给大家供大家参考.具体分析如下: 此方法设置或返回匹配元素的属性值. attr()方法根据参数的不同,功能也不同. 语法结构一: 获取第一个匹配元素指定属性的属性值. 代码如下: $(selector).attr(name) 参数列表: 参数 描述 name 定义要获取其值的属性名称. 实例代码: 代码如下: <!DOCTYPE html> <html> <head> <meta charset="

android style 退出动画 解决退出动画无效问题

在AndroidMenifest.xml文件里面的Activity声明中,增加自己的Theme声明,如下: <activity android:name=".MyOrderListServiceActivity" android:theme="@style/MyTheme" > 在values的style.xml文件里 声明 一个style,起名为MyTheme,注意这里面有我自定义的东西,涉及到动画的声明,就只有 <item name="

android:style.xml

1 <?xml version="1.0" encoding="utf-8"?> 2 <!-- Copyright (C) 2006 The Android Open Source Project 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance wi

如何在android style文件中使用自定义属性

前几天我在项目中遇到了这样一个问题:我为项目编写了一个自定义控件,这个控件会被大量复用,所以我准备在style.xml文件中定义一个style来减少重复xml布局内容的编写,但是里面有一个自定义的控件属性,问题出现在这里,虽然自定义属性在layout布局xml中可以使用正常,但是却无法在style中定义,本来这个控件是大量服用的,style也是为了复用减少xml内容写的,我可以把自定义属性内容直接写死在自定义控件中,但是考虑到项目未来可能出现的变数,我还是准备将这个自定义属性写到style中,这

Android系统自带样式(@android:style/)

在AndroidManifest.xml文件的activity中配置 1.android:theme="@android:style/Theme" 默认状态,即如果theme这里不填任何属性的时候,默认为Theme 2.android:theme="@android:style/Theme.NoDisplay" 任何都不显示.比较适用于只是运行了activity,但未显示任何东西 3.android:theme="@android:style/Theme.

Android自定义属性:attr.xml 与 TypedArray

1.attr.xml <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyView"> <attr name="textColor" format="color" /> <attr name="textSize" format=&q

Android Studio下SVN的用法(1) - 初识SVN

Android Studio下SVN的用法(1) - 初识SVN 前言 这几天公司又来了新人,之前都是一个人开发的项目现在需要合作开发.所以不可避免的需要使用版本控制工具. 公司使用的是SVN,网上有一大堆教程,但是发现对于新手来说,特别是从来没有接触过的人来说,往往都 说的不是很详细,甚至可以说基本没有照顾到这些方面.不要问我为什么这么清楚,因为被坑的不行了--.. PS:本篇文章是介绍AS下SVN的使用,所以可能会跟网上的教程不同 使用 下载及安装 首先前往官网下载SVN客户端,官网上下载可