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

时间:2023-03-08 22:40:55
Android学习笔记(九)——布局和控件的自定义

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

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

一、自定义布局文件

  1)构建布局文件:

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

 <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="5dip"
android:background="@drawable/back_bg"
android:text="Back"
android:textColor="#fff" />
<TextView
android:id="@+id/title_text"
android:layout_width="0dip"
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="5dip"
android:background="@drawable/edit_bg"
android:text="Edit"
android:textColor="#fff" />
</LinearLayout>

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

  2)引入布局文件:

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

 <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>

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

二、自定义控件:

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

  1)新建一个控件类:

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

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

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

 <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"
></com.example.uicustomviews.TitleLayout>
</LinearLayout>

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

  //End.