1、该组合控件的布局文件如下,是由ImageView和TextView组合而成的。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center" >
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:layout_marginLeft="5dp"/>
</LinearLayout>
2、定义一个类,继承于ViewGroup
public class MyButton extends LinearLayout {
private TextView tv;
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
//加载组合布局
LayoutInflater.from(context).inflate(R.layout.mybutton_layout, this,
true);
tv = (TextView) findViewById(R.id.textview);
}
//修改TextView的文字
public void setTextView(String text) {
tv.setText(text);
}
}
3、MainActivity布局中引用
<com.example.customcombin.MyButton
android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
4、要想修改这个自定义组件的内容还需要在MyButton中进行增加修改的方法等。下面的内容就是增加一种方法,直接在com.example.customcombin.MyButton中增加属性修改。
(1)values/attrs.xml文件
<resources>
<declare-styleable name="test">
<!-- 属性的名字和android系统对这属性的描述一致时,不需要format属性 -->
<attr name="android:text" />
<!-- 定义一个名字是textsize的属性,format(单位)值是dimension类型 -->
<attr name="textsize" format="dimension" />
</declare-styleable>
</resources>
(2)MyButton.java中引用
public class MyButton extends LinearLayout {
private TextView tv;
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
//加载组合布局
LayoutInflater.from(context).inflate(R.layout.mybutton_layout, this,
true);
tv = (TextView) findViewById(R.id.textview);
//得到attrs.xml文件的属性数组
TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.test);
//获得属性名字为textsize的值,如果XML文件中没有指明该值,那么就是20
float textSizeFloat = array.getDimension(R.styleable.test_textsize, 20);
tv.setTextSize(textSizeFloat);
//获得名字是android:text的值
String str = array.getString(R.styleable.test_android_text);
tv.setText(str);
array.recycle();
}
}
(3)MainActivity的布局中引用
A.如果要引用的是和系统名字一样的属性,就简单多了。
<com.example.customcombin.MyButton
android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"/>
B.如果引用的是自定义的名字的属性,首先在根标签下增加,
xmlns:haha(标签名)="http://schemas.android.com/apk/res/包名"
然后再引用
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:haha="http://schemas.android.com/apk/res/com.example.customcombin"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!-- 注意textsize属性的写法 -->
<com.example.customcombin.MyButton
android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
haha:textsize="30dp" />
</RelativeLayout>