首先创建一个标题的布局文件
<?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="wrap_content">
<Button
android:id="@+id/title_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:text="Back"
android:textColor="#ff41ff39" />
<TextView
android:id="@+id/title_text"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Title Text"
android:textColor="#ff00"
android:textSize="24sp" />
<Button
android:id="@+id/title_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:text="Edit"
android:textColor="#ffd20fff" />
</LinearLayout>
package com.example.administrator.uicustomview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
/**
* Created by Administrator on 2015/4/15.
*/
public class TitleLayout extends LinearLayout implements View.OnClickListener {
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title_layout, this);
Button backBtn = (Button) findViewById(R.id.title_back);
backBtn.setOnClickListener(this);
Button editBtn = (Button) findViewById(R.id.title_edit);
editBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.title_back:
Toast.makeText(getContext(), "返回", Toast.LENGTH_SHORT).show();
break;
case R.id.title_edit:
Toast.makeText(getContext(), "编辑", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getContext(), "按错了", Toast.LENGTH_SHORT).show();
}
}
}
首先我们重写了LinearLayout中的带有两个参数的构造函数,在布局中引入TitleLayout控件就会调用这个构造函数。
然后在构造函数中需要对标题栏布局进行动态加载,这就要借助LayoutInflater来实现了。通过LayoutInflater的from()方法可以构建出一个LayoutInflater对象,然后调用inflate()方法就可以动态加载一个布局文件。
inflate()方法接收两个参数,第一个参数是要加载的布局文件的id,这里我们传入R.layout.title_layout,第二个参数是给加载好的布局再添加一个父布局,这里我们想要指定为TitleLayout,于是直接传入this。
最后在需要使用的布局文件中引入标题栏布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 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.administrator.uicustomview.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title"></com.example.administrator.uicustomview.TitleLayout>
<!--<include layout="@layout/title_layout" android:id="@+id/title" />-->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:text="@string/nomalBtn"/>
</RelativeLayout>
效果如图: