最近在学习自定义组合控件,看到了一篇文章,照这样子写了一段代码,并做了一些修改。
首先以xml文件为组合控件的基础代码如下:
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_title"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是标题一"/>
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_title"
android:paddingLeft="5dp"
android:text="没有被选中"/>
<CheckBox
android:id="@+id/box_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBottom="@+id/tv_content"
android:layout_marginRight="5dp"
/>
</RelativeLayout>
自定义组件当然可以定义属性了,下面为本次的控件做属性定义:
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomRelative">
<attr name="contenttitle" format="string"/>
<attr name="contenton" format="string"/>
<attr name="contentoff" format="string"/>
</declare-styleable>
</resources>
以上是为本次组件定义的属性,接下来就在控件的构造方法中获取到属性值以便使用。
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by UWorks on 2016/7/12.
*/
public class CustomRelative extends RelativeLayout {
private TextView title;
private TextView content;
private CheckBox box;
private String mTitle;
private String mContenton;
private String mContentoff;
public CustomRelative(Context context, AttributeSet attrs) {
super(context, attrs);
View view = View.inflate(context,R.layout.layout_custom, this);
title = (TextView) view.findViewById(R.id.tv_title);
content = (TextView) view.findViewById(R.id.tv_content);
box = (CheckBox) view.findViewById(R.id.box_status);
TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.CustomRelative);
int n = array.getIndexCount();
for (int i = 0;i<n;i++){
int attr = array.getIndex(i);
switch (attr){
case R.styleable.CustomRelative_contenttitle:
mTitle = array.getString(attr);
Log.d("组合控件",mTitle);
break;
case R.styleable.CustomRelative_contenton:
mContenton = array.getString(attr);
Log.d("组合控件",mContenton);
break;
case R.styleable.CustomRelative_contentoff:
mContentoff = array.getString(attr);
Log.d("组合控件",mContentoff);
break;
}
}
if (mTitle != null){
title.setText(mTitle);
}
if (mContentoff != null){
content.setText(mContentoff);
}
array.recycle();
box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
content.setText(mContenton);
}else {
content.setText(mContentoff);
}
}
});
}
}
这里需要注意的是<pre name="code" class="java">View.inflate(context,R.layout.layout_custom, this);这句话表明在其他布局使用本次定义的控件时,以布局为跟布局,否则将不会显示自定义的组合控件