Android学习笔记(九)——布局和控件的自定义

  //此系列博文是《第一行Android代码》的学习笔记,如有错漏,欢迎指正!

  View是 Android中一种最基本的 UI组件,它可以在屏幕上绘制一块矩形区域,并能响应这块区域的各种事件,因此,我们使用的各种控件其实就是在 View的基础之上又添加了各自特有的功能。而ViewGroup 则是一种特殊的 View,它可以包含很多的子 View和子 ViewGroup,是一个用于放置控件和布局的容器。系统默认的所有控件都是直接或间接继承自 View 的,所用的所有布局都是直接或间接继承自 ViewGroup的。如果系统自带的控件并不能满足我们的需求时,我们可以创建自定义控件。

一、自定义布局文件

  1)构建布局文件:

  我们先新建一个布局文件,添加一些所需的控件,代码如下:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2 android:layout_width="match_parent"
 3 android:layout_height="wrap_content"
 4 android:background="@drawable/title_bg" >
 5 <Button
 6     android:id="@+id/title_back"
 7     android:layout_width="wrap_content"
 8     android:layout_height="wrap_content"
 9     android:layout_gravity="center"
10     android:layout_margin="5dip"
11     android:background="@drawable/back_bg"
12     android:text="Back"
13     android:textColor="#fff" />
14 <TextView
15     android:id="@+id/title_text"
16     android:layout_width="0dip"
17     android:layout_height="wrap_content"
18     android:layout_gravity="center"
19     android:layout_weight="1"
20     android:gravity="center"
21     android:text="Title Text"
22     android:textColor="#fff"
23     android:textSize="24sp" />
24 <Button
25     android:id="@+id/title_edit"
26     android:layout_width="wrap_content"
27     android:layout_height="wrap_content"
28     android:layout_gravity="center"
29     android:layout_margin="5dip"
30     android:background="@drawable/edit_bg"
31     android:text="Edit"
32     android:textColor="#fff" />
33 </LinearLayout>

  我们在布局文件中定义了两个button和一个TextView,其中的android:background是传入res中的图片文件作为控件或布局的背景的。而 android:layout_margin这个属性,可以指定控件在上下左右方向上偏移的距离,当然也可以使用 android:layout_marginLeft或 android:layout_marginTop 等属性来单独指定控件在某个方向上偏移的距离。

  2)引入布局文件:

  当我们想要把自定义好的布局文件引入到自己的活动中时,只需要在加上一行代码即可:  

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:layout_width="match_parent"
3 android:layout_height="match_parent" >
4 <include layout="@layout/title" />
5 </LinearLayout>

   我们在布局文件中加入了<include layout="@layout/title" />,便可以让活动加载此布局,不过记得在活动的将系统自带的标题栏隐藏掉。

二、自定义控件

  很多活动都会有一些功能相同的控件,例如back键等,假如我们在每个活动中为它们单独编写一次事件注册的代码,这无疑会提高代码量。为此,我们可以使用自定义控件来解决此问题。

  1)新建一个控件类:

1 public class TitleLayout extends LinearLayout {
2     public TitleLayout(Context context, AttributeSet attrs) {
3     super(context, attrs);
4     LayoutInflater.from(context).inflate(R.layout.title, this);
5     }
6 }

   代码中我们重写了 LinearLayout 中的带有两个参数的构造函数,在布局中引入 TitleLayout控件就会调用这个构造函数。然后在构造函数中需要对标题栏布局进行动态加载,这就要借助 LayoutInflater 来实现了。通过 LayoutInflater 的 from()方法可以构建出一个 LayoutInflater对象,然后调用 inflate()方法就可以动态加载一个布局文件,inflate()方法接收两个参数,第一个参数是要加载的布局文件的 id,这里我们传入 R.layout.title,第二个参数是给加载好的布局再添加一个父布局,这里我们想要指定为 TitleLayout,于是直接传入 this。

  2)在布局文件中添加此控件:

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:layout_width="match_parent"
3 android:layout_height="match_parent" >
4     <com.example.uicustomviews.TitleLayout
5     android:layout_width="match_parent"
6     android:layout_height="wrap_content"
7     ></com.example.uicustomviews.TitleLayout>
8 </LinearLayout>

   添加自定义控件和添加普通控件的方式基本是一样的, 只不过在添加自定义控件的时候我们需要指明控件的完整类名,包名在这里是不可以省略的。此时我们就可以想使用一般的控件一样在活动中调用这些控件的。

  //End.

原文地址:https://www.cnblogs.com/Vincent-Bryan/p/5384936.html