declare-styleable, 自定义属性

来源:互联网 发布:mac gt330m 黑屏 编辑:程序博客网 时间:2024/06/06 00:34

 

<!-- /* Font Definitions */ @font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:"/@宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:12.0pt;font-family:"Times New Roman";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;} /* Page Definitions */ @page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}@page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;mso-header-margin:36.0pt;mso-footer-margin:36.0pt;mso-paper-source:0;}div.Section1{page:Section1;} /* List Definitions */ @list l0{mso-list-id:187108745;mso-list-type:hybrid;mso-list-template-ids:-1117512042 -446148180 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}@list l0:level1{mso-level-tab-stop:18.0pt;mso-level-number-position:left;margin-left:18.0pt;text-indent:-18.0pt;}@list l1{mso-list-id:300119905;mso-list-type:hybrid;mso-list-template-ids:-7276738 234147932 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}@list l1:level1{mso-level-number-format:alpha-lower;mso-level-tab-stop:39.0pt;mso-level-number-position:left;margin-left:39.0pt;text-indent:-18.0pt;text-decoration:underline;text-underline:single;}ol{margin-bottom:0cm;}ul{margin-bottom:0cm;}-->

1.      LAYOUT文件里定义了控件,如:

<com.motorola.motohome.NewSlidingDrawer

       android:id="@+id/drawer"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

 

       motohome:topOffset="5px"

       motohome:bottomOffset="5px"

       motohome:handle="@+id/all_apps"

       motohome:content="@+id/content"

       motohome:drawable="false"

       motohome:gnbcontent="@+id/gnb_bar">

       任何其控件下的属性,需要在res/values/attrs.xml中定义

       比如android<resources>

         <declare-styleablename="Theme">

           <attr name="id"format="reference" />

           <attrname="layout_width" format="dimension">

              <enumname="fill_parent" value="-1" />

                 <enum name="wrap_content"value="-2" />

             </attr>

      比如自定义的MOTOHOME:

      <resources>

       <declare-styleable name="NewSlidingDrawer">

        <!-- Identifier for the child that represents the drawer's handle. -->

        <attr name="handle"format="reference" />

        <!-- Identifier for the child that represents the drawer's content.-->

        <attr name="content" format="reference"/>

       <!-- add by fang -->

       <attr name="gnbcontent"format="reference" />

       

        <!-- Orientation of the SlidingDrawer. -->

 

        <!-- Extra offset for the handle at the bottom of the SlidingDrawer. -->

       <attr name="bottomOffset" format="dimension"  />

</declare-styleable>

这样你就可以再控件里引用这个属性,并给他赋值了。

然后你可以在此控件里引用它

TypedArray a =context.obtainStyledAttributes(attrs, R.styleable.NewSlidingDrawer, defStyle, 0);

        mDrawable = a.getBoolean(R.styleable.NewSlidingDrawer_drawable, false);

        mVertical = (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);

        mBottomOffset = (int) a.getDimension(R.styleable.NewSlidingDrawer_bottomOffset,0);

a.    recycle();

 

以上情况是属于自己定义,自己赋值,另外一种是预先赋值,比如参看下BUTTON代码

public class Button extends TextView {

    public Button(Context context) {

        this(context, null);

    }

 

    public Button(Context context,AttributeSet attrs) {

        this(context, attrs,com.android.internal.R.attr.buttonStyle);

    }

 

    public Button(Context context,AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

    }

}

用到com.android.internal.R.attr.buttonStyle,从ATTRS.XML文件中找到他

<resources>

     <declare-styleable name="Theme">

<attr name="buttonStyle"format="reference" />

 

在当前目录下找到一个文件THEMES.XML

发现里面有一个定义好的

<style name="Theme">

    <itemname="buttonStyle">@android:style/Widget.Button</item>

 

OK,android自定义的Theme又引用了其他STYLE

再到STYLES中找到

<style name="Widget.Button">

        <itemname="android:background">@android:drawable/btn_default</item>

        <itemname="android:focusable">true</item>

        <itemname="android:clickable">true</item>

        <itemname="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>

        <itemname="android:textColor">@android:color/primary_text_light</item>

        <itemname="android:gravity">center_vertical|center_horizontal</item>

    </style>

 

再次发现android:background属性引用了另外一个DRAWABLE文件

Btn_default

<selectorxmlns:android="http://schemas.android.com/apk/res/android">

    <itemandroid:state_window_focused="false"android:state_enabled="true"

       android:drawable="@drawable/btn_default_normal" />

    <itemandroid:state_window_focused="false"android:state_enabled="false"

       android:drawable="@drawable/btn_default_normal_disable" />

    <itemandroid:state_pressed="true"

       android:drawable="@drawable/btn_default_pressed" />

    <itemandroid:state_focused="true" android:state_enabled="true"

       android:drawable="@drawable/btn_default_selected" />

    <itemandroid:state_enabled="true"

       android:drawable="@drawable/btn_default_normal" />

    <itemandroid:state_focused="true"

       android:drawable="@drawable/btn_default_normal_disable_focused"/>

    <item

        android:drawable="@drawable/btn_default_normal_disable" />

</selector>

注意,最后在LAYOUT里用到自定义属性时,需要

多加一条

<com.motorola.motohome.DragLayer

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:motohome="http://schemas.android.com/apk/res/com.motorola.motohome"

最后要加上包名。