1. attrs.xml 的作用

控件有很多属性,如 android:id、android:layout_width、android:layout_height等,但是这些属性都是系统自带的属性。使用attrs.xml文件,可以自己定义属性,下面我会写些小 demo ,比较好理解

2. 在values文件夹下,新建一个attrs.xml文件

内容如下:

<?xml version="1.0" encoding="utf-8"?><declare-styleable name="MyView">        <attr name="textColor" format="color" />        <attr name="textSize" format="dimension" /> declare-styleable>

其中,

<declare-styleable name="MyView">

表明样式名称为MyView,下面包含了两个自定义属性textColor和textSize,其中textColor是颜色(color)类的属性,textSize是尺寸(dimension)类的属性

3. 自定义 MyView

public class MyView extends View {    private Paint mPaint;    private static final String mString = "Welcome to BaiYe's blog";    public MyView(Context context) {        this(context,null);    }    public MyView(Context context, AttributeSet attrs) {        super(context, attrs);        mPaint = new Paint();        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView);        int textColor = a.getColor(R.styleable.MyView_textColor, 0XFFFFFFFF);        float textSize = a.getDimension(R.styleable.MyView_textSize, 36);        mPaint.setTextSize(textSize);        mPaint.setColor(textColor);        a.recycle();    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        // 设置填充        mPaint.setStyle(Paint.Style.FILL);        // 画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标//        mPaint.setColor(Color.BLACK);        canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);        // 绘制文字        canvas.drawText(mString, 60, 410, mPaint);    }}
  • 首先从 R.styleable.CustomView 获得了TypedArray变量
  • 再用getColor(),getDimension()等方法获取相应的属性值,属性格式为“样式名_属性名”,属性后面的参数是默认值。
  • 获得属性值以后,就可以应用这些属性值。
  • recycle()方法用于返回信号给资源(不懂什么意思)

4. xml 内容

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:test="http://schemas.android.com/apk/res-auto"//一定记得添加前缀     android:id="@+id/activity_attrs_actiity"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.lizi.newset.CustomView.attrs.AttrsActivity">    <com.lizi.newset.CustomView.attrs.MyView        android:id="@+id/myView"        android:layout_height="match_parent"        android:layout_width="wrap_content"        test:textSize="50px"        test:textColor="#ff00ff"/>    />RelativeLayout>

xmlns:test=”http://schemas.android.com/apk/res-auto”一定要添加,添加之后才能在xml中自定义属性

5. 自定义属性

格式如上,其中“xmlns:test”冒号后面是标签名,在下面使用时(只对当前文件可用)

<TextView  test:属性名/>

5.1 reference:参考某一资源ID

<declare-styleable name = "名称">      "background" format = "reference" />declare-styleable>eg:"42dip"     android:layout_height = "42dip"     android:background = "@drawable/图片ID"                     />

5.2 color:颜色值

<declare-styleable name = "名称">       "textColor" format = "color" />declare-styleable>eg: "42dip"     android:layout_height = "42dip"     android:textColor = "#00FF00"                     />

5.3 boolean:布尔值

<declare-styleable name = "名称">      "focusable" format = "boolean" />declare-styleable>eg:

5.4 dimension:尺寸值

"名称">     "layout_width" format = "dimension" />eg:<com.lizi.newset.CustomView.attrs.MyView        android:id="@+id/myView"        android:layout_height="match_parent"        android:layout_width="wrap_content"        test:textSize="50px"        test:textColor="#ff00ff"/>

5.5 float:浮点值

name = "AlphaAnimation">      name = "fromAlpha" format = "float" />      name = "toAlpha" format = "float" />eg:"1.0"       android:toAlpha = "0.7"/>

5.6 string:字符串

"MapView">     "apiKey" format = "string" />eg:<com.google.android.maps.MapView         android:layout_width = "fill_parent"         android:layout_height = "fill_parent"         android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"                    />

5.7 integer:整型值 || fraction:百分数

name = "AnimatedRotateDrawable">      name = "visible" />      name = "frameDuration" format="integer" />      name = "framesCount" format="integer" />      name = "pivotX"  format = "fraction"/>      name = "pivotY"  format = "fraction"/>      name = "drawable" />eg:"http://schemas.android.com/apk/res/android"        android:drawable = "@drawable/图片ID"        android:pivotX = "50%"        android:pivotY = "50%"        android:framesCount = "12"        android:frameDuration = "100"  />

5.8 enum:枚举值

"名称">                   "orientation">                          <enum name="horizontal" value="0" />                          <enum name="vertical" value="1" />                               eg:"http://schemas.android.com/apk/res/android"                    android:orientation = "vertical"                    android:layout_width = "fill_parent"                    android:layout_height = "fill_parent"                    >

5.9 flag 位或运算

 name="名称">                    name="windowSoftInputMode">                            name = "stateUnspecified" value = "0" />                            name = "stateUnchanged" value = "1" />                            name = "stateHidden" value = "2" />                            name = "stateAlwaysHidden" value = "3" />                            name = "stateVisible" value = "4" />                            name = "stateAlwaysVisible" value = "5" />                            name = "adjustUnspecified" value = "0x00" />                            name = "adjustResize" value = "0x10" />                            name = "adjustPan" value = "0x20" />                            name = "adjustNothing" value = "0x30" />                              eg:name = ".StyleAndThemeActivity"      android:label = "@string/app_name"      android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">                  name = "android.intent.action.MAIN" />            name = "android.intent.category.LAUNCHER" />      

6. 属性定义时可以同时定义多种类型值

<declare-styleable name = "名称">      "background" format = "reference|color" />declare-styleable>eg:"42dip"        android:layout_height = "42dip"        android:background = "@drawable/图片ID|#00FF00"        />

“`

更多相关文章

  1. 【Android】TextView常用属性
  2. Android属性之build.prop,及property_get/property_set && Androi
  3. Android API指南(二)自定义控件04之 位置说明
  4. Android的RelativeLayOut中各种属性的作用

随机推荐

  1. 现代浏览器探秘(part 1):架构 [每日前端夜
  2. 讲真,Markdown有啥魅力,竟让如此多人入迷!
  3. 深入探讨 Undefined [每日前端夜话(0x0E)
  4. 《大前端吊打面试官系列》之备战面试篇!
  5. 现代浏览器探秘(part2):导航[每日前端夜
  6. 用Python和Tableau对母婴商品销量进行数
  7. 浅议 Promise/Futures 模型 [每日前端夜
  8. 1-20
  9. 现代浏览器探秘(part3):渲染 [每日前端夜
  10. 自学系列 | 就谈知识体系!