本文参考文章:http://www.cnblogs.com/ivan-xu/p/4545929.html?utm_source=tuicool
在该文章的原有基础上做了部分修改
自定义控件可以分为两种:继承控件及组合控件。前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式;后者是通过组合已有的控件,来实现结构的简化和代码的重用。
本篇文章主要介绍自定义组合控件。
自定义组合控件一般来说都是以ViewGroup及其子类(LinearLayout、RelativeLayout、FrameLayout等)为主,内部嵌套其他控件,来组合成一个新的控件,实现一些特定的需要,可以是代码简化,结构清晰,重用性较高。
通常来说,我们会实现定义好一个Layout.xml文件,然后让我们的自定义控件去加载此xml,并获取子控件,然后设置属性(可以通过代码,也可以从资源文件中加载)、添加事件。
自定义要点:
1.加载xml文件是在构造方法中完成的,通过调用inflate(R.layout.my_layout,this,true),注意第二个和第三个参数;
2.如果需要从资源文件中加载自定义的属性,则必须重写Constructor(Context context, AttributeSet attrs)此构造方法,属性是定义在attrs.xml中的;
3.获取子控件对象,可以在构造方法中获取,也可以重写onFinishInflate()方法来获取,个人建议采用第二种,可以保证控件已经完全加载好了;
4.添加事件可以直接在控件中写,不过考虑到扩展性及复用性,建议对外暴露接口。
自定义控件layout:custom_title.xml
<?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="match_parent"
android:background="#107EE5"
android:id="@+id/rl"
>
<Button
android:id="@+id/btn_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/select_bg"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dip"
android:layout_centerVertical="true"
android:text="右键"
/>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="18sp"
android:text="我是标题"
/>
<Button
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/select_bg"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dip"
android:text="左键"
/>
</RelativeLayout>
自定义控件类:MyRelativeLayout.java
package com.example.customtitle.widget;
import com.example.customtitle.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MyRelativeLayout extends RelativeLayout {
private Button btnLeft,btnRight;
private TextView tv_title;
private RelativeLayout rl;
private String title;//标题
private String btnRightText,btnLeftText;//左右button的文字
private int textColor;//标题颜色
private int backgroundColor;//标题栏背景色
private float textSize;//标题字体大小
public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
//加载视图的布局
View view=LayoutInflater.from(context).inflate(R.layout.custom_title, this, true);
//该方法获取values下attrs.xml下name="MyRelativeLayout"的所有属性集合
TypedArray arr=context.obtainStyledAttributes(attrs, R.styleable.MyRelativeLayout);
//获取MyRelativeLayout控件对应属性信息
title=arr.getString(R.styleable.MyRelativeLayout_titleText);
textColor=arr.getColor(R.styleable.MyRelativeLayout_textColor, Color.BLACK);
textSize=arr.getDimension(R.styleable.MyRelativeLayout_textSize, 18.0f);
btnRightText=arr.getString(R.styleable.MyRelativeLayout_btnRightText);
btnLeftText=arr.getString(R.styleable.MyRelativeLayout_btnLeftText);
backgroundColor=arr.getColor(R.styleable.MyRelativeLayout_backgroundColor,Color.BLUE);
//回收资源,这一句必须调用
arr.recycle();
}
/**
* 此方法会在所有的控件都从xml文件中加载完成后调用
*/
@Override
protected void onFinishInflate() {
// TODO Auto-generated method stub
super.onFinishInflate();
//获取该空间的子控件
btnLeft=(Button) findViewById(R.id.btn_left);
btnRight=(Button) findViewById(R.id.btn_right);
tv_title=(TextView) findViewById(R.id.tv_title);
rl=(RelativeLayout) findViewById(R.id.rl);
//设置子控件的属性
if(!TextUtils.isEmpty(title)){
setTitle(title);
}
if(!TextUtils.isEmpty(btnRightText)){
setBtnRightText(btnRightText);
}
if(!TextUtils.isEmpty(btnLeftText)){
setBtnLeftText(btnLeftText);
}
setTextColor(textColor);
setTextSize(textSize);
setBackgroundColor(backgroundColor);
}
/**
* 设置标题栏背景颜色
*/
public void setBackgroundColor(int backgroundColor) {
rl.setBackgroundColor(backgroundColor);
}
/**
* 设置右边按钮文字内容
*/
public void setBtnRightText(String btnRightText) {
btnRight.setText(btnRightText);
}
/**
* 设置左边按钮文字内容
*/
public void setBtnLeftText(String btnLeftText) {
btnLeft.setText(btnLeftText);
}
/**
* 设置标题文字
*/
public void setTitle(String title) {
tv_title.setText(title);
}
/**
* 设置标题字体颜色
*/
public void setTextColor(int textColor) {
tv_title.setTextColor(textColor);
}
/**
* 设置标题字体大小
*/
public void setTextSize(float textSize) {
tv_title.setTextSize(textSize);
}
/**
* 设置标题栏左边按钮监听事件
*/
public void setLeftButtonClick(OnClickListener onClickListener){
btnLeft.setOnClickListener(onClickListener);
}
/**
* 设置标题栏右边按钮监听事件
*/
public void setRightButtonClick(OnClickListener onClickListener){
btnRight.setOnClickListener(onClickListener);
}
}
自定义属性文件:values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 自定义属性,format属性表示该属性的单位 -->
<declare-styleable name="MyRelativeLayout">
<!-- title文字内容 -->
<attr name="titleText" format="string"/>
<!-- 右边button文字内容 -->
<attr name="btnRightText" format="string"/>
<!-- 左边button文字内容 -->
<attr name="btnLeftText" format="string"/>
<!-- title文字颜色 -->
<attr name="textColor" format="color"/>
<!-- title文字大小 -->
<attr name="textSize" format="dimension"/>
<!-- 标题栏背景颜色 -->
<attr name="backgroundColor" format="color" />
</declare-styleable>
</resources>
以下是引用方式,activity布局文件:activity_main.xml
注意上面XML中代码: xmlns:app = “http://schemas.android.com/apk/res/com.example.customtitle“,是自定义的app命名空间,res后面是应用程序包名,然后可以直接使用app:titleText=”一览众山小”,等属性,其值类型要和attrs.xml定义的属性Value值相对应。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app = "http://schemas.android.com/apk/res/com.example.customtitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<com.example.customtitle.widget.MyRelativeLayout
android:id="@+id/mll"
android:layout_width="match_parent"
android:layout_height="60dip"
app:titleText="一览众山小"
app:textSize="8sp"
app:textColor="#fff"
app:btnRightText="李飞"
app:btnLeftText="李浩"
app:backgroundColor="#2ABA3B"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18dip"
android:text="@string/hello_world" />
</LinearLayout>
Activity类:MainActivity.java
package com.example.customtitle;
import com.example.customtitle.widget.MyRelativeLayout;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends Activity {
private MyRelativeLayout mll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mll=(MyRelativeLayout) findViewById(R.id.mll);
mll.setBtnLeftText("煞笔");//可以设置自定义组合控件里的属性等内容
mll.setLeftButtonClick(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "我是左边的按钮", 0).show();
}
});
mll.setRightButtonClick(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "我是右边的按钮", 0).show();
}
});
}
}
运行结果: