1、仿 iPhone 的风格,在界面的顶部放置一个标题栏。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
> <LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#2197db"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"> <Button
android:id="@+id/title_back"
android:layout_width="90dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="返回"
/> <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="标题"
android:textColor="#fff"
android:textSize="24sp"
/>
<Button
android:id="@+id/title_edit"
android:layout_width="90dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="确定"
/> </LinearLayout>
</RelativeLayout>

标题栏布局已经编写完成,剩下的就是如何在程序中使用这个标题栏。

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

然后在 MainActivity 中将系统自带的标题栏隐藏掉

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
}

我们还是需要在每个活动中为这些控件单独编写一次事件注册的代码。比如说标题栏中的返回按钮,其实不管是在哪一个活动中,这个按钮的功能都是相同的,即销毁掉当前活动,这种情况最好是使用自定义控件的方式来解决。

新建自定义的标题栏控件:

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

*/

 

在布局文件中添加这个自定义控件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.xxxxxx.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
></com.example.xxxxxx.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() {
@Override
public void onClick(View v) {
((Activity) getContext()).finish();
}
}); titleEdit.setOnClickListener(new OnClickListener() {
public static final String TAG = ""; @Override
public void onClick(View v) {
Toast.makeText(getContext(), "重新运行程序", Toast.LENGTH_SHORT).show();
Log.i(TAG, "111 ");
}
});
} }

Android-->创建自定义控件的更多相关文章

  1. android创建自定义控件

    新建一个布局title.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xm ...

  2. Android学习之基础知识五—创建自定义控件

    下面是控件和布局的继承关系: 从上面我们看到: 1.所有控件都是直接或间接继承View,所有的布局都是直接或间接继承ViewGroup 2.View是Android中最基本的UI组件,各种组件其实就是 ...

  3. Android中创建自定义控件

    1.创建一个TitleLayout继承LinearLayout: //创建自定义控件 public class TitleLayout extends LinearLayout { private f ...

  4. android#嵌入式布局并创建自定义控件

    一.如何在android中嵌入布局文件: 新建一个布局title.xml,该文件为公共文件 <LinearLayout xmlns:android="http://schemas.an ...

  5. Android Studio 之创建自定义控件

    •前言 常用控件和布局的继承结构,如下图所示: 可以看到,我们所用的所有的控件都是直接或者间接的继承自View的: 所用的所有布局都是直接或者间接继承自ViewGroup的: View 是 Andro ...

  6. 【转】Android中自定义控件的步骤

    原文网址:http://blog.csdn.net/lianchen/article/details/48038969 Android开发中难免遇到需要自定义控件的需求,有些是产品的要求在Androi ...

  7. Android 创建自定义布局

    我们所有的控件都是继承至View类的,而所有的布局都是继承至ViewGroup的,所以我们也可以继承某个view类来实现我们自己的布局或者控件. 引入布局 我们新建一个title.xml的layout ...

  8. 《转载-两篇很好的文章整合》Android中自定义控件

    两篇很好的文章,有相互借鉴的地方,整合到一起收藏 分别转载自:http://blog.csdn.net/xu_fu/article/details/7829721 http://www.cnblogs ...

  9. Android之自定义控件

    开发自定义控件的步骤: 1.了解View的工作原理  2. 编写继承自View的子类 3. 为自定义View类增加属性  4. 绘制控件  5. 响应用户消息  6 .自定义回调函数    一.Vie ...

  10. Andriod - 创建自定义控件

    控件和布局的继承结构: 可以看到,我们所用的所有控件都是直接或间接继承自 View的,所用的所有布局都是直接或间接继承自 ViewGroup 的.View 是 Android 中一种最基本的 UI 组 ...

随机推荐

  1. gitlab 取消注册功能

    gitlab 默认安装完成以后是允许用户注册,公司内部使用所以准备禁用了注册功能,只允许管理员从后台开通权限: 1.进入"Admin Area" 2.在左边菜单栏最低下点击&quo ...

  2. [原创]Nexus5 源码下载、编译、真机烧录过程记录

    asop使用清华镜像源https://mirror.tuna.tsinghua.edu.cn/help/AOSP/ 一开始使用每月初始化包的方式因为无法搞定版本的问题,没能通过编译,无奈,老老实实一点 ...

  3. MyBatis-3.2.2

    note SqlSessionFactory 它是一个线程安全的 SqlSession 线程非安全,不能做类的公用变量 当数据库字段和实体对象名称不一至时,通过sql的字段命名别名,别名跟实体对象属性 ...

  4. [原创]LAMP+phpmyadmin+FTP环境搭建

    ***简单ftp服务器搭建: rpm –qa|grep vsftpd   //检查是否安装服务 yum –y install vsftpd-*   //安装服务 mkdir /var/ftp/uplo ...

  5. PHP实现记录日志(文件)

    PHP实现记录日志(文件) php php 记录日志 项目中经常会记录些操作信息,或是打印些关键变量,或者是导入excel文件,提现记录,都需记录.经常遇到,封装一个方法,有不好的地方或补充请留言. ...

  6. 冒泡排序的python代码实现

    li = [33, 2, 10, 1,564,880,8,99,51,3]# for i in range(len(li) - 1):#     current = li[i]#     next_v ...

  7. [速成]了解一致性hash算法

    定义 一致性hash算法,在维基百科的定义是: Consistent hashing is a special kind of hashing such that when a hash table ...

  8. 更换包管理工具npm为yarn

    官网:https://yarnpkg.com/zh-Hans/ 主要考虑: 1. npm管理安装模块依赖的版本不太方便,容易在删除node_modules重新install或在其他机器上新安装时, 安 ...

  9. 一天搞定HTML----标签语义化04

    根据页面里不同的内容,选择最适合它的标签,而不通篇只用一种标签 标签语义化作用: 代码演示 通过比较- - -H5布局和DIV+CSS 布局- - -体现标签语义化 注意: 标签语义化,不仅仅只是指使 ...

  10. TreeSet集合排序方式一:自然排序Comparable

    TreeSet集合默认会进行排序.因此必须有排序,如果没有就会报类型转换异常. 自然排序 Person class->实现Comparable,实现compareTo()方法 package H ...