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

更多相关文章

  1. Animation介绍
  2. Android5.0 设置wifi页面的修改
  3. Android(安卓)SDK Manager 代理服务器设置
  4. Android(安卓)4.0后,自定义Title报错 You cannot combine custom
  5. android开发给linearlayout设置边框
  6. Android(安卓)M新控件之AppBarLayout,NavigationView,CoordinatorL
  7. webview 设定和使用缓存来获取网页中的js,css和图片资源
  8. android 自定义TextView"会发脾气的TextView"
  9. Android布局学习——android:gravity和android:layout_gravity的

随机推荐

  1. 有关MVC异常情况的相关处理
  2. C#编程如何获取电脑硬件信息的方法?
  3. 从零讲解.Net Core开发实现图片文件上传
  4. Windows登录功能使用C#实现的示例
  5. ASP.NET验证码制作
  6. C#如何连接加密数据库(Sqlite)的示例分享
  7. MVC5限制所有HTTP必须以POST方式请求
  8. 利用ashx生成图形验证码实例教程
  9. 制作NetCore WebSocket即时通讯实例详解
  10. 利用Dapper实现分页效果方法教程