zoukankan      html  css  js  c++  java
  • 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)所定义的属性,那么我们可以在 esvalues里面的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();

  • 相关阅读:
    Array方面Js底层代码学习记录
    DOM 节点
    跨域
    狂雨cms代码审计:后台文件包含getshell
    在PHP一句话木马使用过程中的种种坑点分析
    记对某CMS的一次代码审计
    通达OA任意文件上传并利用文件包含导致远程代码执行漏洞分析
    DedeCMS V5.7 SP2后台存在代码执行漏洞
    zzzcms(php) v1.7.5 前台SQL注入及其他
    权限维持及后门持久化技巧总结
  • 原文地址:https://www.cnblogs.com/chengliu/p/4130614.html
Copyright © 2011-2022 走看看