Andriod: 在xml布局中使用自定义属性

来源:互联网 发布:淘宝网买男鞋 编辑:程序博客网 时间:2024/06/06 03:49

Andriod: 在xml布局中使用自定义属性

1、自定义View的属性,首先在res/values/ 下建立一个attr.xml , 在里面定义我们的属性和声明我们的整个样式。

`<?xml version="1.0" encoding="utf-8"?><resources><attr name="relation">    <enum name="icon_left"     value="0" />    <enum name="icon_right"    value="1" />    <enum name="icon_above"    value="2" />    <enum name="icon_below"    value="3" /></attr><declare-styleable name="IconText">    <attr name="relation" />    <attr name="icon"         format="reference" />    <attr name="text"         format="string" />    <attr name="text_size"     format="dimension" />    <attr name="text_color"    format="integer" />    <attr name="space"         format="dimension" /></declare-styleable></resources>`

解释

属性relation有4种可选值:icon_left, icon_right, icon_above,icon_below.属性icon的可选值为引用: 例如:"@/drawbable/icon".属性text的可选值为string, 例如: "Hello world", 也可是string的引用"@string/hello".属性text_size的可选值为尺寸大小,例如:20sp、18dip、20px等.属性text_color的可选值为整数,例如:"0xfffffff", 也可以是color的引用"@color/white".format还可以指定其他的类型比如;reference   表示引用,参考某一资源IDstring   表示字符串color   表示颜色值dimension   表示尺寸值boolean   表示布尔值integer   表示整型值float   表示浮点值fraction   表示百分数enum   表示枚举值flag   表示位运算

2、在layout布局文件中使用这个自定布局及其属性

` <com.example.myview_day02.MyView    xmlns:IconText="http://schemas.android.com/apk/res/com.example.myview_day02"    IconText:relation="icon_left"    IconText:text="hi,how are you!"    IconText:text_size="12sp"    android:layout_width="wrap_content"    android:layout_height="wrap_content"/>`

解释

我们一般使用如下的值xmlns:icontext=""http://schemas.android.com/apk/res/<你的应用程序的包名>"icontext这个是我们自定属性的名字

3、TypedArray详解

TypedArray实例是个属性的容器,context.obtainStyledAttributes()方法返回得到。AttributeSet是节点的属性集合实例代码package com.example.myview_day02;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.util.Log;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;/** * Created by Administrator on 2016/7/5 0005. */public class MyView extends LinearLayout {    private final static String TAG = "IconTextView";    private final int ICON_LEFT = 0;    private final int ICON_RIGHT = 1;    private final int ICON_ABOVE = 2;    private final int ICON_BELOW = 3;    private TextView mTextView;    private ImageView mImageView;    private int mRelation = ICON_LEFT;    private String mText = "";    private int mIconId;    private float mTextSize;    private int mSpace;    public MyView(Context context, AttributeSet attrs){        super(context, attrs);        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconText);        mRelation = a.getInt(R.styleable.IconText_relation, ICON_LEFT);        Log.d(TAG,"mRelation: "+mRelation);        mText = a.getString(R.styleable.IconText_text);        Log.d(TAG,"mText: "+mText);        mTextSize = a.getDimensionPixelSize(R.styleable.IconText_text_size, 12);        Log.d(TAG,"mTextSize: "+mTextSize);        mSpace = a.getDimensionPixelSize(R.styleable.IconText_space, 5);        Log.d(TAG,"mSpace: "+mSpace);        mIconId = a.getResourceId(R.styleable.IconText_ico, R.drawable.haha);        Log.d(TAG,"mIconId: "+mIconId);        a.recycle();        mTextView = new TextView(context);        mTextView.setText(mText);        mTextView.setTextSize(mTextSize);        mImageView = new ImageView(context);        mImageView.setImageResource(mIconId);        int left    = 0;        int top     = 0;        int right    = 0;        int bottom    = 0;        int orientation = HORIZONTAL;        int textViewIndex = 0;        switch(mRelation){            case ICON_ABOVE:                orientation = VERTICAL;                bottom = mSpace;                textViewIndex = 1;                break;            case ICON_BELOW:                orientation = VERTICAL;                top = mSpace;                break;            case ICON_LEFT:                right = mSpace;                textViewIndex = 1;                break;            case ICON_RIGHT:                left = mSpace;                break;        }        this.setOrientation(orientation);        this.addView(mImageView);        mImageView.setPadding(left, top, right, bottom);        this.addView(mTextView, textViewIndex);    }

}

0 0