atts.xml文件下declare-style自定义控件属性的用法

一、在res/values文件下定义一个attrs.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="ToolBar"> 
        <attr name="buttonNum" format="integer"/> 
        <attr name="itemBackground" format="reference|color"/> 
    </declare-styleable> 
</resources>

二、在布局xml中如下使用该属性:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   
xmlns:toolbar="http://schemas.android.com/apk/res/cn.zzm.toolbar" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <cn.zzm.toolbar.ToolBar android:id="@+id/gridview_toolbar" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:background="@drawable/control_bar" 
        android:gravity="center" 
        toolbar:buttonNum="5" 
        toolbar:itemBackground="@drawable/control_bar_item_bg"/> 
</RelativeLayout>

三、在自定义组件中,可以如下获得xml中定义的值:

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ToolBar); 
buttonNum = a.getInt(R.styleable.ToolBar_buttonNum, 5); 
itemBg = a.getResourceId(R.styleable.ToolBar_itemBackground, -1);

a.recycle();

就这么简单的三步,即可完成对自定义属性的使用。

好了,基本用法已经讲完了,现在来看看一些注意点和知识点吧。

首先来看看attrs.xml文件。

该文件是定义属性名和格式的地方,需要用<declare-styleable name="ToolBar"></declare-styleable>包围所有属性。其中name为该属性集的名字,主要用途是标识该属性集。那在什么地方会用到呢?主要是在第三步。看到没?在获取某属性标识时,用到"R.styleable.ToolBar_buttonNum",很显然,他在每个属性前面都加了"ToolBar_"。

在来看看各种属性都有些什么类型吧:string , integer , dimension , reference , color , enum......

前面几种的声明方式都是一致的,例如:<attr name="buttonNum" format="integer"/>。 
只有enum是不同的,用法举例:

<attr name="testEnum"> 
    <enum name="fill_parent" value="-1"/> 
    <enum name="wrap_content" value="-2"/> 
</attr>

如果该属性可同时传两种不同的属性,则可以用“|”分割开即可。

 

让我们再来看看布局xml中需要注意的事项。

首先得声明一下:xmlns:toolbar=http://schemas.android.com/apk/res/cn.zzm.toolbar 
注意,“toolbar”可以换成其他的任何名字,后面的url地址必须最后一部分必须用上自定义组件的包名。自定义属性了,在属性名前加上“toolbar”即可。

 

最后来看看java代码中的注意事项。

在自定义组件的构造函数中,用

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ToolBar);

来获得对属性集的引用,然后就可以用“a”的各种方法来获取相应的属性值了。这里需要注意的是,如果使用的方法和获取值的类型不对的话,则会返回默认值。因此,如果一个属性是带两个及以上不用类型的属性,需要做多次判断,知道读取完毕后才能判断应该赋予何值。当然,在取完值的时候别忘了回收资源哦!

 

自定义属性数据类型简介:

 

一、reference:参考指定Theme中资源ID。

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="label" format="reference" >
    </declare-styleable>

2.使用:

1
    <Buttonzkx:label="@string/label" >

二、Color:颜色

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="textColor" format="color" />
    </declare-styleable>

2.使用:

1
    <Button zkx:textColor="#ff0000"/>

三、boolean:布尔值

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="isVisible" format="boolean" />
    </declare-styleable>

2.使用:

1
    <Button zkx:isVisible="false"/>

四、dimension:尺寸值

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="myWidth" format="dimension" />
    </declare-styleable>

2.使用:

1
    <Button zkx:myWidth="100dip"/>

五、float:浮点型

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="fromAlpha" format="float" />
    </declare-styleable>

2.使用:

1
    <alpha zkx:fromAlpha="0.3"/>

六、integer:整型

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="frameDuration" format="integer" />
    </declare-styleable>

2.使用:

1
    <animated-rotate zkx:framesCount="22"/>

七、string:字符串

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="Name" format="string" />
    </declare-styleable>

2.使用:

1
    <rotate zkx:pivotX="200%"/>

八、fraction:百分数

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="pivotX" format="fraction" />
    </declare-styleable>

2.使用:

1
    <rotate zkx:Name="My name is zhang kun xiang"/>

九、enum:枚举

1.定义:

1
2
3
4
5
    <declare-styleable name="My">
        <attr name="language">
            <enum name="English" value="1"/>
        </attr>
    </declare-styleable>

2.使用:

1
    <Button zkx:language="English"/>

十、flag:位或运算

1.定义:

1
2
3
4
5
6
    <declare-styleable name="My">
        <attr name="windowSoftInputMode">
    	<flag name="stateUnspecified" value="1" />
    	<flag name = "adjustNothing" value = "0x30" />
        </attr>
    </declare-styleable>

2.使用:

1
    <activity android:windowSoftInputMode="stateUnspecified | adjustNothing">

属性定义时可以指定多种类型值:

1
2
3
    <declare-styleable name = "名称">    
	<attr name="background" format="reference|color" />
    </declare-styleable>

使用:

1
    <ImageView android:background = "@drawable/图片ID|#00FF00"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以为两张表的 atts.only 和 aots.only 字段分别创建索引,以提高查询速度。可以使用以下 SQL 语句为这两个字段创建索引: ``` ALTER TABLE analy_total_tables ADD INDEX idx_only (only); ALTER TABLE analy_out_tables ADD INDEX idx_only (only); ``` 这样创建索引后,查询语句会使用索引来查找匹配的数据,提高了查询速度。同时,还可以使用 EXPLAIN 命令来分析查询语句的执行计划,以确定是否使用了索引等优化方式。例如: ``` EXPLAIN SELECT atts.cWhCode as 仓库编码, atts.cWhName as 仓库名称, atts.cInvCCode as 存货分类代码, atts.cInvCName as 存货分类名称, atts.cInvCode as 存货编码, atts.cInvStd as 规格型号, atts.cInvName as 存货名称, atts.cBatch as 批号, atts.cFree1 as 片号, atts.iNum as 入库件数, aots.iNum as 出库件数, atts.iQuantity as 入库数量, aots.iQuantity as 出库数量, atts.cFree4 as 厚度, atts.cFree5 as 背面金属, atts.cInvDefine5 as 供应商名称, atts.cInvDefine6 as 芯片来源, atts.cPosCode as 货位编码, atts.cPosName as 货位名称, atts.cInvDefine2 as Wafer尺寸, atts.dDate as 入库日期, atts.iinvexchrate as 换算率, atts.cFree3 as 外观属性, atts.cInvDefine4 as 供应商型号 FROM analy_total_tables atts LEFT JOIN analy_out_tables aots ON atts.only = aots.only; ``` 执行该命令后,可以查看到查询语句的执行计划,包括使用了哪些索引等信息。根据执行计划的结果,可以进一步优化查询语句和索引,以提高查询速度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值