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

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   表示引用,参考某一资源ID
string   表示字符串
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);
    }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值