Android备忘录 -- 自定义控件

来源:互联网 发布:网络结构图图标 编辑:程序博客网 时间:2024/06/05 05:07

1.引入布局

    当有一些重复出现的控件配置(如统一的标题栏)出现时,为了避免代码重复,就需要使用  引入布局  了。

    下面以标题栏为例子说明

    1.1 新建布局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="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>
    1.2 引入title.xml,即标题栏

<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 语句就可将标题栏布局引入了。

    1.3 隐藏系统自带标题栏

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_main);    }}
    程序运行效果图如下所示

引入标题栏
2. 创建自定义控件

    引入布局后,经常还需要为布局的控件注册响应时间,这时也是为了避免代码重复,需要用到  自定义控件  。

    下面仍以自定义标题栏为例。

    1. 新建自定义控件 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中的带有2个参数的构造函数,在布局中引入了TitleLayout这个自定义控件就会调用这个构造函数了。然后在构造函数中对标题栏布局进行动态加载。

        上面代码中,通过LayoutInflater的from()方法可以构建出一个LayoutInflater对象,然后调用inflate()方法就可以动态加载一个布局文件。inflate()方法接收两个参数,第一个参数是要加载的布局文件的ID,第二个参数是加载文件的父级布局。

    2. 在活动布局文件中添加自定义控件

        修改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>
        如代码所示, 添加自定义控件和添加普通控件的方式基本是一样的,只是添加自定义控件时需要指明控件的完整类名。

    3.为标题栏的按钮注册点击事件

        具体示例代码如下

public class TitleLayout extends LinearLayout{    public TitleLayout(Context context, Attributes 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();}});}}
        程序效果图如下

自定义标题栏-输出