对于android中自定义组件都会采用三种方式来实现,即自定义View、组合组件、继承已有的组件。。不论我们何种方式来实现自定义组件,有些时候我们需要自定义我们组件中的属性,我在使用的时候总结如下:
1.首先,先写attrs.xml
在res-vlaues文件夹下创建资源文件attrs.xml或则自定义一个资源文件xx.xml,都可以。
之后在里面配置declare-styleable
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyEditTextView">
<attr name="textValue" format="reference|string" />
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
<declare-styleable name="MyButton">
<attr name="btnValue" format="reference|string" />
<attr name="btnbg" format="reference|color" />
</declare-styleable>
</resources>
format就是格式,里面的就是这个属性对应的格式,下面列出来大致的格式有:
1. reference:参考某一资源ID,以此类推
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
</declare-styleable>
(2)属性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID"
/>
2. color:颜色值
<declare-styleable name = "名称">
<attr name = "textColor" format = "color" />
</declare-styleable>
3. boolean:布尔值
<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
4. dimension:尺寸值。注意,这里如果是dp那就会做像素转换
<declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
5. float:浮点值。
6. integer:整型值。
7. string:字符串
8. fraction:百分数。
9. enum:枚举值
10. flag:是自己定义的,类似于 android:gravity="top",就是里面对应了自己的属性值。
注意://由于reference是从资源文件中获取:所以在XML文件中写这个属性的时候必须 personattr:name="@string/app_name"这种格式,否则会出错
2.设置好属性文件后,在使用的布局中写相关配置:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:jone="http://schemas.android.com/apk/res/com.example.customview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.customview.MyEditTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
jone:orientation="vertical"
jone:textValue="from value" >
</com.example.customview.MyEditTextView>
<com.example.customview.MyEditTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
jone:orientation="vertical"
jone:textValue="@string/str_atrr" >
</com.example.customview.MyEditTextView>
<com.example.customview.MyButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
jone:btnValue="@string/str_atrr"
jone:btnbg="@drawable/bottom_bar" >
</com.example.customview.MyButton>
<com.example.customview.MyButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
jone:btnValue="from value" >
</com.example.customview.MyButton>
</LinearLayout>
这里要先应用这个attr:
xmlns:jone="http://schemas.android.com/apk/res/com.example.customview"
后面为你的包名
之后在布局中自定义的类中设相关属性:
你自己定义的名称:你设的属性 ="属性值";
package com.example.customview;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.Button;
/**
*
* 〈一句话功能简述〉<br>
* 〈功能详细描述〉
* 自定义Button属性demo
* @author 14052012
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class MyButton extends Button {
String str;
int background;
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.MyButton);
int COUNT = ta.getIndexCount();
int resourceId = -1;
for (int i = 0; i < COUNT; i++) {
int attr = ta.getIndex(i);
switch (attr) {
case R.styleable.MyButton_btnValue:
resourceId = ta.getResourceId(
R.styleable.MyButton_btnValue, 0);
setText(resourceId > 0 ? ta.getResources().getText(
resourceId) : ta.getString(resourceId));
break;
case R.styleable.MyButton_btnbg:
resourceId = ta
.getResourceId(R.styleable.MyButton_btnbg, 0);
setBackgroundDrawable(resourceId > 0 ? ta.getResources()
.getDrawable(resourceId) : ta
.getDrawable(resourceId));
break;
}
resourceId = -1;
}
}
}
package com.example.customview;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
*
* 〈一句话功能简述〉<br>
* 〈功能详细描述〉
* 自定义EditTextView属性demo
* @author 14052012
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class MyEditTextView extends LinearLayout {
public MyEditTextView(Context context, AttributeSet attrs) {
super(context, attrs);
int resourceId = -1;
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.MyEditTextView);
TextView tv = new TextView(context);
EditText ed = new EditText(context);
int Count = ta.getIndexCount();
for (int i = 0; i < Count; i++) {
int attr = ta.getIndex(i);
switch (attr) {
case R.styleable.MyEditTextView_textValue:
// 判断format是否输入reference ,若属于reference,则按照resource方式取数据
// ta.getResources().getText(resourceId)
// 否则,按照正常方式取数据
// ta.getString(resourceId)
resourceId = ta.getResourceId(
R.styleable.MyEditTextView_textValue, 0);
tv.setText(resourceId > 0 ? ta.getResources().getText(
resourceId) : ta.getString(resourceId));
break;
case R.styleable.MyEditTextView_orientation:
resourceId = ta.getInt(
R.styleable.MyEditTextView_orientation, 0);
this.setOrientation(resourceId == 0 ? LinearLayout.HORIZONTAL
: LinearLayout.VERTICAL);
break;
}
resourceId = -1;
}
addView(tv);
addView(ed);
ta.recycle();
}
}
附一个我的demo