一、关于View
1、View:所有用到的控件或者布局都是直接继承或者间接继承于View。它可以在屏幕上绘制一块矩形区域,并响应整块区域的各种事件,所以我们使用的控件都是在View的基础上又添加了各自的功能
2、View的工作机制
经过measure、layout和draw三个过程才可以把一个View绘制出来,其中measure用来测量View的宽高,layout用来确定View在父容器中的放置位置,而draw则负责将View绘制到屏幕上。
二、引入布局
1、相当于定义一个布局模板。定义一个布局title.xml(这是一个标题栏的布局,两个按钮一个文本框)
2、要是某个布局想引入这个布局,只需在这个布局里添加
<include layout="@layout/title"/>
三,创建自定义组件
1、意义,要是某个控件的响应事件是常用的,那么就把这个控件封装成一个自定义控件(也可以说是类,也可以说是一个布局)这里表示的很模糊,直接看代码吧
(1)下面这是之前设置好的一个标题布局(也就是上文说的模板布局)
<?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"
android:background="@drawable/title_bg">
<Button
android:id="@+id/title_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/back_bg"
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_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/edit_bg"
android:text="Edit"
android:textColor="#fff" />
</LinearLayout>
因为基本所有的标题栏里的左面Button的功能都是销毁活动,为了不让每一次引入这个标题栏的时候都要添加响应事件,那么就把这个布局(两个Button一个TextView封装成一个自定义控件)这个自定义控件以类的形式被定义
(在这里说明一个自定义控件一般继承两种情况)
(1)继承于ViewGroup(ViewGroup,LinearLayout,RelativeLayout,FrameLayout等)
(2)继承于View(View,TextView,ImageView,Button等)
为什么解释自定义控件又可以说是一个类或者布局呢(当然这是我自己的理解)
(1)因为自定义控件是以类的方式定义的,其实这个也不难理解,其实我们现在用的所有控件例如Button本身也是一个类,不过这个类的功能已经被封装好了,只提供我们一个Buton接口就好了,其实应该计算机的产品都是这样的,实现一个功能,我们最后只把这个功能的接口提交给用户就好了。所以这自定义的控件需要我们自己写内部的方法实现,最后我们用的时候只需要用这个控件的名字(也可以说是这个控件的接口或者这个控件的类名就可以),这就像别人帮我们写好Button这个控件的内部实现,我们直接拿来用是一个道理。
(2)因为者自定义控件可以继承与一个布局,下面这个就是继承于LinearLayout。所以也可以说自定义控件是一个布局,(毕竟无论布局还是控件都是View的子类,所以这样说自定义控件可以看成一个布局也是可以的)
话不多说,直接看代码
package com.example.uicustomviews;
import android.app.Activity;
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;
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
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 v) {
((Activity) getContext()).finish();
}
});
titleEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "You clicked Edit button",
Toast.LENGTH_SHORT).show();
}
});
}
}
注意下面这一行
LayoutInflater.from(context).inflate(R.layout.title, this);
这是构造函数对标题栏布局进行动态加载。这就要借助LayoutInflater来实现。通过LayoutInflater的from()方法可以构建出一个LayoutInflater的实例,然后调用inflate方法就可以动态加载一个布局文件。inflate两个参数,第一个参数就是要加载布局的id,第二个就是给加载好的布局再添加一个父布局,这里指定为TitleLayout,于是直接传入this。
重点来了!!!!
那么我们现在已经封装好了一个自定义控件(TitleLayout),俺么如何在布局中利用到呢
<?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" >
<com.example.uicustomviews.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
布局中引用。所以说控件和布局穿插使用。自定义控件中引入了布局,当控件定义好了之后又要用到布局当中。
总结
所以以后用到TitleLayout控件的布局中,都会自动有两个Button和一个TextView且有事件响应。
所以自定义控件就是自己定义一个类,然后在这里进行操作(包括导入这个控件的组成,布局样式,以及事件响应),然后直接用这个控件。