<LinearLayout 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:orientation="vertical" >
<include
android:id="@+id/title"
layout="@layout/title"
/>
<TextView
android:textSize="15sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这个是首页" />
<com.example.demo1.BottomTitle
android:id="@+id/buttomtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</com.example.demo1.BottomTitle>
</LinearLayout>
在这个 主activity的布局文件中 有两个标签 include和 com.example.demo1.BottomTitle
如果是要把一个简单的布局文件变成一个view 只需要用include标签导入这个layout文件
如果是一个自定义控件(有自己实现的功能比如点击就变色或者切换fragment等等) 可以new一个class 继承控件或者布局 在他的构造方法中描绘这个自定义控件
public class BottomTitle extends LinearLayout {
public BottomTitle(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public BottomTitle(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
setOrientation(LinearLayout.HORIZONTAL);
Button button1 = new Button(getContext());
button1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
button1.setText("首页");
addView(button1);
Button button2 = new Button(getContext());
button2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
button2.setText("帮助");
addView(button2);
}
}
参考文章 : 点击打开链接