组合控件的意思就是用系统原本的简单控件组合成一个复杂的view
首先创建一个布局文件
<?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="50dp"
android:orientation="vertical"
android:background="#ffcb05"
>
<Button
android:id="@+id/button_left"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="#ffcb05"
android:text="Back"
android:textColor="#fff" />
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="This is Title"
android:textColor="#fff"
android:textSize="20sp" />
</RelativeLayout>
然后创建一个java类 继承一个布局类 自绘view是继承View 这个类用来承载上面的布局
package com.example.apple.javademo.javaClass;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.apple.javademo.R;
/**
* Created by apple on 15/12/10.
*/
public class MyCustomView extends LinearLayout
{
Button btn;
TextView textView;
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
//把xml布局好的layout添加到MyCustomView
LayoutInflater.from(context).inflate(R.layout.custom_layout,this);
TypedArray array = context.getTheme().obtainStyledAttributes(attrs,R.styleable.MyCustomView,0,0);
String title = array.getString(R.styleable.MyCustomView_mtitle);
String btnTitle = array.getString(R.styleable.MyCustomView_mtext);
btn = (Button) findViewById(R.id.button_left);
btn.setText(btnTitle);
textView = (TextView)findViewById(R.id.title_text);
textView.setText(title);
}
//按钮事件回调
public void setOnClickButtonListen(OnClickListener onClickListener)
{
btn.setOnClickListener(onClickListener);
}
//标题点击回调
public void setOnClickTitleListen(OnClickListener onClickTitleListen)
{
textView.setOnClickListener(onClickTitleListen);
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyCustomView">
<attr name="mtext" format="string" />
<attr name="mtitle" format="string" />
</declare-styleable>
</resources>
Activity.xml使用如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<!--<com.example.apple.javademo.javaClass.myview-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--app:showText_1="true"-->
<!--app:labelPosition_1="left"-->
<!--app:textSize1="40.0"-->
<!--app:texts="这是自绘控件myview,属性传入文本"-->
<!--/>-->
<com.example.apple.javademo.javaClass.MyCustomView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mtext="返回"
app:mtitle="这是标题"
android:id="@+id/myCustomeV"
>
</com.example.apple.javademo.javaClass.MyCustomView>
</RelativeLayout>
Activity.java内部获取点击事件
MyCustomView myCustomView = (MyCustomView)findViewById(R.id.myCustomeV);
myCustomView.setOnClickTitleListen(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dialog("点击了title按钮");
}
});
myCustomView.setOnClickButtonListen(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dialog("点击了button按钮");
}
});