declare-styleable, 自定义属性

 

<!-- /* 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-styleable name="Theme">

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

           <attr name="layout_width" format="dimension">

              <enum name="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">

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

 

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

再到 STYLES 中找到

<style name="Widget.Button">

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

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

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

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

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

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

    </style>

 

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

Btn_default

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

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

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

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

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

    <item android:state_pressed="true"

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

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

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

    <item android:state_enabled="true"

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

    <item android: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"

最后要加上包名。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值