创建自定义控件

来源:互联网 发布:java常用算法手册 宋娟 编辑:程序博客网 时间:2024/06/11 09:02

        先看一下控件和布局的继承结构,如下图。

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

        这个时候我们就可以思考一下,如果系统自带的控件并不能满足我们的需求,可不可以利用上面的继承结构来创建自定义控件呢?答案是肯定的,下面我们就来学习一下创建自定义控件的两种简单方法。

1.  引入布局

        创建一个标题栏布局应该不是什么困难的事情,只需要加入两个 Button 和一个 TextView,然后在布局中摆放好就可以了。可是这样做却存在着一个问题,一般我们的程序中可能有多个 Activity 都需要这样的标题栏,如果在每个 Activity 的布局中都编写一遍同样的标题栏代码,明显就会导致代码的大量重复。这个时候我们就可以使用引入布局的方式来解决这个问题。新建一个布局 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/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="This is Title"        android:textColor="#fff"        android:textSize="22sp" />    <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>

       编写完成标题栏的布局后,只需要通过一行 include 语句将标题栏布局引入进来就可以了。代码如下所示:

<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 中将系统自带的标题栏隐藏掉。代码如下所示:

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    requestWindowFeature(Window.FEATURE_NO_TITLE);    setContentView(R.layout.activity_main);}

        使用这种方式,不管有多少布局需要添加标题栏,只需要一行 include 语句就可以了。

3.4.2  创建自定义控件

        引入布局的技巧确实解决了重复编写布局代码的问题,但是如果布局中有一些控件要求能够响应事件,我们还是需要在每个 Activity 中为这些控件单独编写一次事件注册的代码。比如说标题栏中的返回按钮,其实不管在哪一个 Activity 中,这个按钮的功能都是相同的,即销毁掉当前 Activity。而如果在每一个 Activity 中都需要重新注册一遍返回按钮的点击事件,无疑又是增加了很多重复代码,这种情况最好是使用自定义控件的方式来解决。

        新建 TitleLayout 继承自 LinearLayout,让它成为我们自定义的标题栏控件。代码如下所示:

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。

        现在自定义控件已经创建好了,然后我们需要在布局文件中添加这个自定义控件,修改 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.uicustomviews.TitleLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        ></com.example.uicustomviews.TitleLayout></LinearLayout>

        添加自定义控件和添加普通控件的方式基本是一样的,只不过在添加自定义控件的时候我们需要指明控件的完整类名,包名在这里是不可以省略的。

        然后我们俩尝试为标题栏中的按钮注册点击事件,修改 TitleLayout 中的代码,如下所示:

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() {@Overridepublic void onClick(View v) {((Activity) getContext()).finish();}});titleEdit.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getContext(), "You clicked Edit button", Toast.LENGTH_SHORT).show();}});}}

        首先还是通过 findViewById() 方法得到按钮的实例,然后分别调用 setOnClickListener() 方法给两个按钮注册了点击事件,当点击返回按钮时销毁掉当前的 Activity,当点击编辑按钮时弹出一段文本。

        这样的话,每当我们在一个布局中引入 TitleLayout,返回按钮和编辑按钮的点击事件就已经自动实现好了,也是省去了很多编写重复代码的工作。

摘自《第一行代码》

0 0
原创粉丝点击