打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android declare-styleable:自定义控件的属性(attr.xml,TypedArray)的使用

以launcher为例说明自定义控件的属性:

1、  在layout里面定义控件,如:

<com.junction.launcher.DragLayer

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

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

    android:id="@+id/drag_layer" android:layout_width="match_parent"  android:layout_height="match_parent">

  <com.android.launcher.Workspace

        android:id="@+id/workspace"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        launcher:defaultScreen="1">


        <include android:id="@+id/cell1" layout="@layout/workspace_screen" />

        <include android:id="@+id/cell2" layout="@layout/workspace_screen" />

        <include android:id="@+id/cell3" layout="@layout/workspace_screen" />

    </com.android.launcher.Workspace>

2、  该控件workspace下面有一个属性是launcher:defaultScreen,这属性不是ViewGroup(workspace类是继承于ViewGroup)所定义的属性,那么我们可以在\res\values里面的attrs.xml里面定义

具体的定义方法如下:

<resources>

<declare-styleable name="Workspace">

    <attr name="defaultScreen" format="integer"  />

    </declare-styleable>

</resources>

引用方法是 先申明xmlns:launcher="http://schemas.android.com/apk/res/com.junction.launcher"(R.java),这样就可以使用launcher:defaultScreen。

3、  这样我们在Workspace的类里面就可以去除在layout里面对该属性的赋值,方法如下:

Int mDefaultScreen;

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

mDefaultScreen = a.getInt(R.styleable.Workspace_defaultScreen, 1);

a.recycle();

++++++++++++++++++++++++++++++

自己案例:可看一个gifPlayer的demo程序。

其他案例:http://blog.csdn.net/Android_Tutor/archive/2010/04/20/5508615.aspx

++++++++++++++++++++++++++++++

转载自:http://blog.csdn.net/tinafhx/archive/2010/02/05/5290878.aspx

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
TypedArray和attrs.xml
Android declare
declare-styleable是给自定义控件添加自定义属性用的
android自定义控件——以滑动开关为例
Android自定义View(二、深入解析自定义属性)
自定义属性都没有掌握好,都不好意思说自己是Android开发者
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服