自定义安卓控件
一、效果图如下:
二、自定义控件的步骤
a) 自定义布局文件,这个布局文件就是该控件的XML布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="68dip"
android:background="@color/background_color" >
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="8dip"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:layout_marginLeft="10dip"
android:textColor="#88000000"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dip"
android:clickable="false"
android:contentDescription="这里是个‘>’箭头型的图片"
android:focusable="false"
android:src="@drawable/jiantou1_pressed" />
<View
android:layout_width="fill_parent"
android:layout_height="0.2dip"
android:layout_alignParentBottom="true"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:background="#000000" />
</RelativeLayout>
b) 在项目资源目录值目录下即res/values目录下,建立该自定义控件的属性值名为attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextView">
<attr name="title" format="string" />
<attr name="desc_on" format="string" />
<attr name="desc_off" format="string" />
<attr name="desc" format="string" />
</declare-styleable>
</resources>
c) 在相应的源码包下建立自定义控件类继承布局类
package com.example.phoneguard.ui;
import com.example.phoneguard.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TextView;
/**
* 我们自定义的组合控件,它里面有两个TextView ,还有一个CheckBox,还有一个View
*
* @author EboyWang
*
*/
public class SettingRadioGroupView extends RelativeLayout {
// 声明标题控件
private TextView tv_title;
// 声明描述控件
private TextView tv_desc;
// 声明描述字段
private String desc;
/**
* 初始化布局文件
*
* @param context
*/
private void iniView(Context context) {
// 把一个布局文件---》View 并且加载在SettingItemView
View.inflate(context, R.layout.setting_radio_group_view, this);
// 找到标题和描述控件
tv_desc = (TextView) this.findViewById(R.id.tv_desc);
tv_title = (TextView) this.findViewById(R.id.tv_title);
}
/**
*
* @param context上下文
* @param attrs自定义控件对应的属性文件
* @param defStyle引用的样式
*/
public SettingRadioGroupView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
iniView(context);
}
/**
* 带有两个参数的构造方法,布局文件使用的时候调用
*
* @param context上下文
* @param attrs自定义控件对应的属性文件
*/
public SettingRadioGroupView(Context context, AttributeSet attrs) {
super(context, attrs);
iniView(context);
// 定义字段对应的命名空间和属性名
String title = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/com.example.phoneguard",
"title");
desc = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/com.example.phoneguard",
"desc");
tv_title.setText(title);
// 设置描述信息
setDesc(desc);
}
public SettingRadioGroupView(Context context) {
super(context);
iniView(context);
}
/**
* 设置 组合控件的描述信息
*/
public void setDesc(String text) {
tv_desc.setText(text);
}
}
d) 在布局文件中使用该自定义控件
1.在布局文件中引入在自定义控件类里面声明的命名空间
2.标签名为自定义控件类的全路径,并使用到了在头文件声明使用的pg命名空间头,和在属性文件中指定的desc、title属性
总结:
在安卓里面使用自定义控件非常简单。不过第一次使用我还是颇费了些周折。我在这里描述的只是一个思路,其实可以简单的定义很多复杂的控件。