Android自定义之属性(二)

最近开始研究自定义,依旧参考大神们的博客,今晚说说自定义控件的属性自定义

首先来说一下自定义属性中的format值,及其使用

从最常见的说起string

属性定义:<attr name="text" format="string"/>

属性使用:android:text="@string/hello_world"

其实跟Android中text的使用一样,一般在Android代表文字的一般写在string.xml,这里format的值你就应该知道什么意思

color也是很常见的

属性定义:<attr name="firstColor" format="color"/>

属性使用:android:textColor="@android:color/black"

个人理解,Android建议属性值写在那个xml肯定有它的道理,这里个人理解一般自己的color的值都写在color.xml,应该是跟这里的format相对应

一般color和size搭配使用,接下来dimension

属性定义:<attr name="textSize" format="string"/>

属性使用:android:textSize="16dp"

我理解的跟上面一样,这样也好记,在values中不是有个dimens.xml就对应的这个值

下面说一下referent

属性定义:<attr name = "background" format = "reference|color" />

属性使用:android:background="图片id|颜色值"

这里一起说一下,由于背景可以是颜色,也可以是图片,当背景为颜色是format使用color,当背景为图片时format使用reference

下面我们继续,来说说boolean

属性定义:<attr name="focusable" format="boolean"/>

属性使用:android:focusable="false" 这个很简单,看看就能懂,就不多废话

其余的跟上面的都差不多,这里说一个特殊的枚举enum

属性定义:<attr name="orientation">
                          <enum name="horizontal" value="0" />
                          <enum name="vertical" value="1" />
                   </attr> 

属性使用:android:orientation = "vertical"

最后再来一个flag

属性定义:<attr name="windowSoftInputMode">
                            <flag name = "stateUnspecified" value = "0" />
                            <flag name = "stateUnchanged" value = "1" />
                            <flag name = "stateHidden" value = "2" />
                            <flag name = "stateAlwaysHidden" value = "3" />
                            <flag name = "stateVisible" value = "4" />
                            <flag name = "stateAlwaysVisible" value = "5" />
                            <flag name = "adjustUnspecified" value = "0x00" />
                            <flag name = "adjustResize" value = "0x10" />
                            <flag name = "adjustPan" value = "0x20" />
                            <flag name = "adjustNothing" value = "0x30" />
                     </attr> 

属性使用:android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden"


下面继续说我们今天的主题自定义属性

1.在res/values下新建attrs.xml文件,声明自定义属性的名称和取值

<declare-styleable name="MyView">
        <attr name="firstColor" format="color"/>
        <attr name="secondColor" format="color"/>
        <attr name="circleWidth" format="dimension"/>
        <attr name="speed" format="integer"/>
    </declare-styleable>
2.在代码中遍历自定义属性

贴出核心代码,其余的都跟这个差不多

TypedArray array = context.obtainStyledAttributes(attrs,
				R.styleable.MyView, defStyleAttr, 0);

		int n = array.getIndexCount();
		for (int i = 0; i < n; i++) {
			int attr = array.getIndex(i);

			switch (attr) {
			case R.styleable.MyView_firstColor:
				firstColor = array.getColor(attr, Color.RED);
				break;
			case R.styleable.MyView_secondColor:
				secondColor = array.getColor(attr, Color.GREEN);
				break;
			case R.styleable.MyView_circleWidth:
				circleWidth = array.getDimensionPixelSize(attr,
						(int) TypedValue.applyDimension(
								TypedValue.COMPLEX_UNIT_DIP, 16, getResources()
										.getDisplayMetrics()));
				break;
			case R.styleable.MyView_speed:
				speed=array.getInt(attr, 20);
				break;
			}
		}

		array.recycle();
在布局文件中使用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.sdufe.thea.guo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.sdufe.thea.guo.view.MyView
        android:id="@+id/custom_myview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        custom:firstColor="@android:color/holo_blue_dark"
        custom:secondColor="@android:color/holo_green_light"
        custom:circleWidth="20dp"
        custom:speed="30"/>
</RelativeLayout>
这里需要注意custom,xmlns:custom="http://schemas.android.com/apk/res/自己的包名"

ok,先介绍到这,多多批评...




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值