您的位置:首页 > 移动开发 > Android开发

android学习笔记16 - TypedArray自定义属性

2015-11-27 16:29 525 查看
TypedArray用于自定义标签属性。

在目录res/value下面创建一个attr.xml文件,该文件中包含若干个attr集合,就是所有用户自定义TypedArray:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="InputView">
<attr name="label" format="string" />
<attr name="special" format="boolean" />
<attr name="refrence" format="boolean" />
<attr name="background" format="string"></attr>
<attr name="editType" format="string"></attr>
</declare-styleable>
<declare-styleable name="TipView">
<attr name="fee" format="string" />
<attr name="type">
<enum name="input" value="0" />
<enum name="confirm" value="1" />
<enum name="card" value="2" />
</attr>
</declare-styleable>


包含若干个declare-styleable标签:

<declare-styleable name="InputView">中name定义了变量的名称

<attr name="label" format="string" />标示属性名是label,format指定了该属性类型为string,只能表示字符串。

format还可以指定其他的类型比如:

reference 表示引用,参考某一资源ID

string 表示字符串

color 表示颜色值

dimension 表示尺寸值

boolean 表示布尔值

integer 表示整型值

float 表示浮点值

fraction 表示百分数

enum 表示枚举值

flag 表示位运算

调用方式如下:

首先需要定义一个视图,无论是自定义的view还是系统控件,以下以自定义的LinearLayout为例:

package com.example.parcelableserializable;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class InputView extends LinearLayout implements OnFocusChangeListener,
OnClickListener {

private String label;

private TextView labelTv;

private LayoutParams labelLp;
public InputView(Context context, AttributeSet attrs)
{
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.InputView);
label = (String) a.getText(R.styleable.InputView_label);
a.recycle();
// TODO Auto-generated constructor stub

labelTv = new TextView(this.getContext());
labelTv.setTextColor(Color.rgb(255, 0, 255));
labelTv.setGravity(Gravity.CENTER_VERTICAL);
labelTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
labelTv.setText(label);
labelTv.setPadding(20, 0, 20, 0);

setPadding(20, 0, 20, 0);
setGravity(Gravity.CENTER_VERTICAL);
setOrientation(LinearLayout.HORIZONTAL);
setClickable(true);
setOnClickListener(this);

labelLp = new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.FILL_PARENT);
this.addView(labelTv, labelLp);

}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub

}
@Override
public void onFocusChange(View v, boolean hasFocus)
{
// TODO Auto-generated method stub

}
}


修改构造函数context通过调用obtainStyledAttributes方法来获取一个TypeArray,obtainStyledAttributes(int[] attrs)其参数直接styleable中获得

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.InputView);

label = (String) a.getText(R.styleable.InputView_label);

绑定该label,调用结束后务必调用recycle()方法。

使用这个自定义控件:

<com.example.parcelableserializable.InputView
android:id="@+id/input_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:label="测试"
/>
这个app是未重写的标签名。效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: