安卓中的编写自定义控件
来源:互联网 发布:人力资源矩阵图 编辑:程序博客网 时间: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:
3.经上面的步骤,自定义的布局已经建好了,然后若需要在布局文件中添加这个自定义控件,则activity_main.xml文件如下: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(); } }); }}
<?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>添加自定义控件和添加普通控件一样,只不过需要加上完整的类名即可。
这样的话,我们需要使用这个标题的时候可以直接使用,省去了很多的代码。
阅读全文
0 0
- 安卓中的编写自定义控件
- 自定义类似于安卓中的Toast控件
- 自定义安卓控件
- 安卓----自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓 如何自定义 AlertDialog 控件中的内容
- 安卓自定义控件,自定义控件属性
- (安卓)自定义控件,自定义(view)中的自定义属性
- 安卓自定义日历控件
- 安卓自定义日历控件
- 安卓自定义控件 - 进度条
- 安卓时间控件自定义
- 什么是P问题、NP问题和NPC问题
- 什么是Elasticsearch
- angular中的那些事儿---过滤器
- 关于RAM的IP核调用理解
- 面试题26:复杂链表的复制
- 安卓中的编写自定义控件
- 吝啬的国度--深度优先搜索
- Elasticsearch 5.4 Documents API
- 数组
- 数据存储的常用结构 堆栈、队列、数组、链表
- JQuery编写的定时器实现时钟
- 开启Tomcat APR运行模式,优化并发性能
- 面试题7:用两个栈实现队列
- elasticsearch5.4.1 安装