android自定义视图属性(atts.xml,TypedArray)学习

       最近在学习过程中遇到这个问题,不知道TypedArray是干嘛用的?去官方网站看一下吧:TypedArray继承自Object类,然后再看下它的类概述:

Container for an array of values that were retrieved with obtainStyledAttributes(AttributeSet, int[], int, int) or obtainAttributes(AttributeSet, int[]). Be sure to call recycle() when done with them. The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes.




是一个用于存放恢复 obtainStyledAttributes( AttributeSet, int[], int, int )或   obtainAttributes(AttributeSet, int[])   值的一个数组容器,当操作完成以后,一定要调用recycle()方法。 用于检索的索引值在这个结构对应的位置给obtainStyledAttributes属性。

        使用这个类的时候,先要在valuse文件夹下创建:atts.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="FlowIndicator">
        <attr name="count" format="integer" />
        <attr name="space" format="dimension" />
        <attr name="point_size" format="dimension" />
        <attr name="point_seleted_color" format="color|reference" />
        <attr name="point_normal_color" format="color|reference" />
        <attr name="point_radius" format="dimension" />
    </declare-styleable>
</resources>

         首先,声明自定义<declare-styleable name="FlowIndicator">,nameFlowIndicator,属性设置为比较简单的格式,前面参数name,后面是参数格式。

自定义属性的format,可以有以下多种:

  • reference
  • string
  • color
  • dimension
  • boolean
  • integer
  • float
  • fraction
  • enum
  • flag

然后这样使用:

public FlowIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		//获得实例
		TypedArray typeArray = context.obtainStyledAttributes(attrs,
				R.styleable.FlowIndicator);
		//从typeArray获取相应值,第二个参数为默认值,如第一个参数在atts.xml中没有定义,返回第二个参数值
		count = typeArray.getInteger(R.styleable.FlowIndicator_count, 4);
		space = typeArray.getDimension(R.styleable.FlowIndicator_space, 9);
		radius = typeArray.getDimension(R.styleable.FlowIndicator_point_radius, 9);

		point_normal_color = typeArray.getColor(
				R.styleable.FlowIndicator_point_normal_color, 0x000000);
		point_seleted_color = typeArray.getColor(
				R.styleable.FlowIndicator_point_seleted_color, 0xffff07);

		int sum = attrs.getAttributeCount();
		if (Constans.DEBUG) {
			String str = "";
			for (int i = 0; i < sum; i++) {
				String name = attrs.getAttributeName(i);
				String value = attrs.getAttributeValue(i);
				str += "attr_name :" + name + ": " + value + "\n";
			}
			Log.i("attribute", str);
		}
		typeArray.recycle();
	}


最后一定不要忘记typeArray.recycle():

Give back a previously retrieved StyledAttributes, for later re-use.

给回以前提取的styledattributes,以后再使用。

应该注意到,获取属性的时候所用的R.styleable.FlowIndicator_count中的FlowIndicator_count是采取的名字_属性这种格式。

定义好了自定义属性,就可以在自定控件中的属性设置了:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.dream.myqiyi"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
        <com.dream.myqiyi.widget.FlowIndicator
            android:id="@+id/myView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dip"
            app:count="4"
            android:gravity="center"
            app:point_normal_color="#45000000"
            app:point_radius="3dip"
            app:point_seleted_color="#ffffff"
            app:point_size="5dip"
            app:space="10dip" />
</FrameLayout>

首先,要有声明:

 xmlns:app="http://schemas.android.com/apk/res/com.dream.myqiyi",“com.dream.myqiyi”这个是你项目的包名。

然后我们就可以使用app:这样设置自定义的属性了。

 app:point_normal_color="#45000000"
            app:point_radius="3dip"
            app:point_seleted_color="#ffffff"
            app:point_size="5dip"
            app:space="10dip"


先简单的写到这吧,如果问题,欢迎探讨。


  • 9
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
可以为两张表的 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; ``` 执行该命令后,可以查看到查询语句的执行计划,包括使用了哪些索引等信息。根据执行计划的结果,可以进一步优化查询语句和索引,以提高查询速度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值