常用控件和布局的继承结构
自定义控件
1.引入布局
新建一个布局title.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title1">
<Button
android:id="@+id/title_back"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/title1"
android:text="Back"
android:textColor="#fff"/>
<TextView
android:id="@+id/title_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Title Text"
android:textColor="#fff"
android:textSize="24sp"/>
<Button
android:id="@+id/title_edit"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/title1"
android:text="Edit"
android:textColor="#fff"/>
</LinearLayout>
background用于为布局或者控件指定一个背景,可以使用颜色或者图片来进行填充。
layout_margin指定控件在上下左右方向上的偏移的距离,当然也可以使用layout_marginLeft或者layout_marginTop等属性来单独指定控件在某个方向上偏移的距离。
修改activity_main.xml中的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/title"/>
</LinearLayout>
在MainActivity中将系统自带的标题栏隐藏掉
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar=getSupportActionBar();
if (actionBar != null){
actionBar.hide();
}
}
自定义控件
为了防止需要在每一个活动中为相同的控件单独编写一次事件注册的代码
新建TitleLayout继承自LinearLayout,让它成为自定义的标题栏控件
public class TitleLayout extends LinearLayout{
public TitleLayout(Context context, AttributeSet attributeSet){
super(context,attributeSet);
LayoutInflater.from(context).inflate(R.layout.title,this);
}
}
重写了LinearLayout中带有两个参数的构造函数,在布局中引用TitleLayout控件就会调用这个构造函数,然后借助LayoutInflater来实现对标题栏布局的动态加载。通过LayoutInflater的from()方法可以构建出一个LayoutInflater对象,然后调用inflate()方法就可以实现动态加载一个布局文件,inflate()方法接收两个参数,第一个参数是要加载的布局文件的id,第二个参数是给加载好的布局再添加一个父布局。
自定义控件创建好后,在布局文件activity_main.xml中中添加这个自定义控件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.amanda.uicustomviews.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
添加自定义控件和添加普通控件的方式基本是一样的,只不过在添加自定义控件的时候需要指明空间的完整类名,包名不可省略
为标题栏中的按钮注册点击事件,修改TitleLayout中的代码:
public class TitleLayout extends LinearLayout{
public TitleLayout(Context context, AttributeSet attributeSet){
super(context,attributeSet);
LayoutInflater.from(context).inflate(R.layout.title,this);
Button titleBack =(Button) findViewById(R.id.title_back);
Button titleEdit =(Button) findViewById(R.id.title_edit);
titleBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
((Activity) getContext()).finish();
}
});
titleEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(),"You clicked Edit button",Toast.LENGTH_SHORT).show();
}
});
}
}