TypedArray和attrs.xml

TypedArray和attrs.xml和AttributeSet这一系列都是自定义控件属性时要用到的内容。

首先说说attrs.xml:它是定义成类似于这种形式的。


1.主要讲讲里面的format属性:

①. reference:参考某一资源ID。

(1)属性定义:

(2)属性使用:

android:layout_width="42dip"

android:layout_height="42dip"

android:background="@drawable/图片ID"

/>

②. color:颜色值。

(1)属性定义:

(2)属性使用:

android:layout_width="42dip"

android:layout_height="42dip"

android:textColor="#00FF00"

/>

③. boolean:布尔值。

(1)属性定义:

(2)属性使用:

android:layout_width="42dip"

android:layout_height="42dip"

android:focusable="true"

/>

④. dimension:尺寸值。

(1)属性定义:

(2)属性使用:

android:layout_width="42dip"

android:layout_height="42dip"

/>

⑤. float:浮点值。

(1)属性定义:

(2)属性使用:

android:fromAlpha="1.0"

android:toAlpha="0.7"

/>

⑥. integer:整型值。

(1)属性定义:

(2)属性使用:

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

android:drawable="@drawable/图片ID"

android:pivotX="50%"

android:pivotY="50%"

android:framesCount="12"

android:frameDuration="100"

⑦. string:字符串。

(1)属性定义:

(2)属性使用:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"

/>

⑧. fraction:百分数。

(1)属性定义:

(2)属性使用:

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

android:interpolator="@anim/动画ID"

android:fromDegrees="0"

android:toDegrees="360"

android:pivotX="200%"

android:pivotY="300%"

android:duration="5000"

android:repeatMode="restart"

android:repeatCount="infinite"

/>

⑨. enum:枚举值。

(1)属性定义:

(2)属性使用:

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

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

10. flag:位或运算。

(1)属性定义:

(2)属性使用:

android:name=".StyleAndThemeActivity"

android:label="@string/app_name"

android:windowSoftInputMode="stateUnspecified | stateUnchanged | stateHidden">

注意:

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

(1)属性定义:

(2)属性使用:

android:layout_width="42dip"

android:layout_height="42dip"

android:background="@drawable/图片ID|#00FF00"

/>

下面是一个布局文件:


version="1.0"encoding="utf-8"?>

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

//自定义的View的路径为com.xc.demo

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

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

//这里引用 改变字体和颜色 textColor和textSize都是根据前面的attrs.xml文件设置的

test:textSize="100dp"

test:textColor="#ff0000"

/>

2.TypedArray的作用是在代码中设置。

publicMyView(Context context, AttributeSet attrs) {

this(context);

TypedArray a = context

.obtainStyledAttributes(attrs, R.styleable.MyView);

intcolor = a.getColor(R.styleable.MyView_textColor,0XFF0000FF);

floatsize = a.getDimension(R.styleable.MyView_textSize,50);

mPaint.setColor(color);

mPaint.setTextSize(size);

a.recycle();

}

R.styleable.MyView是attrs.xml中的名字

具体的设置是依靠名字+"_"+名字得到来设置的。

3.如果代码和xml中都设置了,一般以xml中设置为先。


http://5200415.blog.51cto.com/3851969/1012267

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转载请标明出处:http://www.aiuxian.com/article/p-1982467.html 接下来...
    lucky_yaya阅读 6,844评论 3 9
  • 最近在做软件从2.3到4.0的改变的一些工作,其中涉及了一些style和theme相关的东西。上网上查了一些东西,...
    Ten_Minutes阅读 1,124评论 0 2
  • 首先描述下需求: 1、新增短信时,进来收件人是为空,显示一行文字(提醒) 2、从通讯录选择联系人后,回到短信编辑界...
    小学生写代码阅读 669评论 0 0
  • 一枚中年的叶子 他发黄、生斑、卷曲 在初冬里面黄肌瘦 他念叨雨水这场盛宴 迟了大半辈子 早已无法再把他 干瘪的身体...
    归舴阅读 393评论 6 11
  • 你有没有什么时候发现自己在较长的一段时间之内发现虽然有无数的代办事项在你身后,但是自己却不知道应该从何做起。 是的...
    LuryYang阅读 154评论 0 1