安卓中的编写自定义控件

来源:互联网 发布:人力资源矩阵图 编辑:程序博客网 时间:2024/06/07 05:08

标题栏中的返回按钮,其实不管在哪一个活动中,这个按钮的功能都是相同的,即摧毁当前活动,而如果我们在每一个活动中都需要重新注册一遍返回按钮的点击事件,无疑会增加很多代码,最后的情况是使用自定义控件。

1.首先定义一个通用的布局title.xml:

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

2.新建一个继承自LinearLayout的类TitleLayout.java:

package com.example.wanglunhui.titilelayout;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) {//重写了LinearLayout的构造函数,则在布局中引入这个布局的时候就会调用这个构
//造函数,然后再构造函数中需要对标题栏布局进行动态加载,这就需要用到LayoutInflater来实现。通过LayoutInflater的from()方法可以构造出一个
//LayoutInflater对象,然后调用inflate()方法可以动态加载一个布局文件,inflate()接受两个参数,一个是要加载的布局layout文件,另一个是给加载好的
//布局再添加一个父布局,这里我们指定想要TitleLayout,于是直接传入this。        super(context, attrs);        LayoutInflater.from(context).inflate(R.layout.title, this);        Button titleBack = (Button) findViewById(R.id.title_back);//上面已经加载了这个布局,所以这里是在R.layout.title中获取的两个Button        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();            }        });    }}

3.经上面的步骤,自定义的布局已经建好了,然后若需要在布局文件中添加这个自定义控件,则activity_main.xml文件如下:

<?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>
添加自定义控件和添加普通控件一样,只不过需要加上完整的类名即可。

这样的话,我们需要使用这个标题的时候可以直接使用,省去了很多的代码。

原创粉丝点击