开发项目时发现页面顶部都是高度一致的,直接上图
都是一个返回键,加一个文本框 后边一坨textview和图标。
最开始时候是把这个布局文件抽取出来了,在页面中用include方式引入。但是这样还是需要处理返回键逻辑和链接WiFi逻辑。于是决定自定义组合控件
步骤如下:
先建一个topview类,继承LinearLayout
public class TopView extends LinearLayout
创建构造方法,把布局文件引进来
public TopView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.top_views, this); }
编写布局文件
<?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="match_parent"
android:background="@color/black"
android:id="@+id/top_view"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/iconback">
</ImageView>
<TextView
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"
android:layout_gravity="center_vertical"
android:textColor="@color/Yellow">
</TextView>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_co