一、如何在android中嵌入布局文件:

新建一个布局title.xml,该文件为公共文件

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:background="@drawable/title_bg" >
  5. <Button
  6. android:id="@+id/title_back"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_gravity="center"
  10. android:layout_margin="5dip"
  11. android:background="@drawable/back_bg"
  12. android:text="Back"
  13. android:textColor="#fff" />
  14. <TextView
  15. android:id="@+id/title_text"
  16. android:layout_width="0dip"
  17. android:layout_height="wrap_content"
  18. android:layout_gravity="center"
  19. android:layout_weight="1"
  20. android:gravity="center"
  21. android:text="Title Text"
  22. android:textColor="#fff"
  23. android:textSize="24sp" />
  24. <Button
  25. android:id="@+id/title_edit"
  26. android:layout_width="wrap_content"
  27. 第一行代码——Android
  28. 128
  29. android:layout_height="wrap_content"
  30. android:layout_gravity="center"
  31. android:layout_margin="5dip"
  32. android:background="@drawable/edit_bg"
  33. android:text="Edit"
  34. android:textColor="#fff" />
  35. </LinearLayout>

我们在主页面中添加如下代码:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent" >
  4. <include layout="@layout/title" />
  5. </LinearLayout>

即可引入公共布局文件

二、如何创建自定义控件

在以上代码的基础上新建如下代码:

  1. public class TitleLayout extends LinearLayout {
  2. public TitleLayout(Context context, AttributeSet attrs) {
  3. super(context, attrs);
  4. LayoutInflater.from(context).inflate(R.layout.title, this);
  5.  
  6. }
  7. }

并将主页面代码修改为

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent" >
  4. <com.example.uicustomviews.TitleLayout
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. ></com.example.uicustomviews.TitleLayout>
  8. </LinearLayout>

将自定义控件的代码进行修改

  1. public class TitleLayout extends LinearLayout {
  2. public TitleLayout(Context context, AttributeSet attrs) {
  3. super(context, attrs);
  4. LayoutInflater.from(context).inflate(R.layout.title, this);
  5. Button titleBack = (Button) findViewById(R.id.title_back);
  6. Button titleEdit = (Button) findViewById(R.id.title_edit);
  7. titleBack.setOnClickListener(new OnClickListener() {
  8. @Override
  9. public void onClick(View v) {
  10. ((Activity) getContext()).finish();
  11.  
  12. }
  13. });
  14. titleEdit.setOnClickListener(new OnClickListener() {
  15. @Override
  16. public void onClick(View v) {
  17. Toast.makeText(getContext(), "You clicked Edit button",
  18. Toast.LENGTH_SHORT).show();
  19. }
  20. });
  21. }
  22. }

这样,在每次引入自定义控件自后,对应的按钮事件绑定就已经完成,省去编写重复代码。

android#嵌入式布局并创建自定义控件的更多相关文章

  1. Android开发系列之创建自定义控件

    Android开发过程中我们经常需要定义自己的控件,一方面基于复用的角度考虑,一方面也是基于逻辑处理思维的角度考虑.在这篇博客里面,笔者想要介绍.总结几种Android自定义控件的方法,如果有什么不对 ...

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

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

  3. Android中创建自定义控件

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

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

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

  5. Android四大组件之Activity(活动)及其布局的创建与加载布局

    Android四大组件之Activity(活动)及其布局的创建与加载布局 什么是Activity ? 活动(Activity)是包含用户界面的组件,主要用于和用户进行交互的,一个应用程序中可以包含零个 ...

  6. 让我们创建屏幕- Android UI布局和控件

    下载LifeCycleTest.zip - 278.9 KB 下载ViewAndLayoutLessons_-_Base.zip - 1.2 MB 下载ViewAndLayoutLessons_-_C ...

  7. Android入门(四)UI-创建自定义控件

    原文链接:http://www.orlion.ga/441/ 一.引入布局 iphone应用顶部会有一个标题栏,我们可以模仿着做一个,但是如果我们的程序中很多个活动都需要这样的标题栏,如果 每一个活动 ...

  8. Android群英传笔记——第三章:Android控件架构与自定义控件讲解

    Android群英传笔记--第三章:Android控件架构与自定义控件讲解 真的很久没有更新博客了,三四天了吧,搬家干嘛的,心累,事件又很紧,抽时间把第三章大致的看完了,当然,我还是有一点View的基 ...

  9. Andriod - 创建自定义控件

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

随机推荐

  1. JavaScript中的类(Class)

    基本上,ES6的class可以看作只是一个语法糖,它的绝大部分功能,ES5都可以做到的,新的class写法是让对象原型的写法更加清晰,更像面向对象编程的语法而已. ES5生成例对象传统方法是通过构造函 ...

  2. python基于opencv实现人脸定位

    import cv2 # 读取图片 img = cv2.imread("image.jpg") # 加载模型,模型可以从https://github.com/opencv/open ...

  3. Java HashMap实现原理分析

    参考链接:https://www.cnblogs.com/xiarongjin/p/8310011.html 1. HashMap的数据结构 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是 ...

  4. 015_linuxC++之_覆写

    34.类成员函数的重载.覆盖和隐藏区别?答案:a.成员函数被重载的特征:(1)相同的范围(在同一个类中):(2)函数名字相同:(3)参数不同:(4)virtual 关键字可有可无.b.覆盖是指派生类函 ...

  5. linux下pyenv的安装和使用

    一:pyenv介绍   项目地址:https://github.com/pyenv/pyenv   pyenv lets you easily switch between multiple vers ...

  6. istio 安装与bookinfo示例运行

    目的 本文旨在帮助想了解istio安装和运行bookinfo示例的同学快速入门 前置准备 安装k8s和helm 1.k8s安装 修改主机名 hostnamectl set-hostname k8s-m ...

  7. OI程序常见的设计陷阱

    宏定义的问题 有时候为了方便,我会大量使用宏定义.但是最近我发现下面这两个宏定义老是出问题: #define SET(x,a) memset(x,a,sizeof(x)) inline void wo ...

  8. synchronized的对象锁和类锁

    概念 synchronized 是 Java 中的关键字,是利用锁的机制来实现同步的. 锁机制有如下两种特性: 互斥性:即在同一时间只允许一个线程持有某个对象锁,通过这种特性来实现多线程中的协调机制, ...

  9. axios中出现两次请求,OPTIONS请求和GET请求

    在项目中发现ajax中出现两次请求,OPTIONS请求和GET请求 查看到浏览器NetWork有两次请求,请求url一样: 查找原因是浏览器对简单跨域请求和复杂跨域请求的处理区别. XMLHttpRe ...

  10. FutureTask用法及解析

    1 FutureTask概念 FutureTask一个可取消的异步计算,FutureTask 实现了Future的基本方法,提空 start cancel 操作,可以查询计算是否已经完成,并且可以获取 ...