android#嵌入式布局并创建自定义控件
一、如何在android中嵌入布局文件:
新建一个布局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
- 128
- 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>
我们在主页面中添加如下代码:
- <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>
即可引入公共布局文件
二、如何创建自定义控件
在以上代码的基础上新建如下代码:
- public class TitleLayout extends LinearLayout {
- public TitleLayout(Context context, AttributeSet attrs) {
- super(context, attrs);
- LayoutInflater.from(context).inflate(R.layout.title, this);
- }
- }
并将主页面代码修改为
- <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>
将自定义控件的代码进行修改
- 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() {
- @Override
- public void onClick(View v) {
- Toast.makeText(getContext(), "You clicked Edit button",
- Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
这样,在每次引入自定义控件自后,对应的按钮事件绑定就已经完成,省去编写重复代码。
android#嵌入式布局并创建自定义控件的更多相关文章
- Android开发系列之创建自定义控件
Android开发过程中我们经常需要定义自己的控件,一方面基于复用的角度考虑,一方面也是基于逻辑处理思维的角度考虑.在这篇博客里面,笔者想要介绍.总结几种Android自定义控件的方法,如果有什么不对 ...
- Android学习之基础知识五—创建自定义控件
下面是控件和布局的继承关系: 从上面我们看到: 1.所有控件都是直接或间接继承View,所有的布局都是直接或间接继承ViewGroup 2.View是Android中最基本的UI组件,各种组件其实就是 ...
- Android中创建自定义控件
1.创建一个TitleLayout继承LinearLayout: //创建自定义控件 public class TitleLayout extends LinearLayout { private f ...
- Android Studio 之创建自定义控件
•前言 常用控件和布局的继承结构,如下图所示: 可以看到,我们所用的所有的控件都是直接或者间接的继承自View的: 所用的所有布局都是直接或者间接继承自ViewGroup的: View 是 Andro ...
- Android四大组件之Activity(活动)及其布局的创建与加载布局
Android四大组件之Activity(活动)及其布局的创建与加载布局 什么是Activity ? 活动(Activity)是包含用户界面的组件,主要用于和用户进行交互的,一个应用程序中可以包含零个 ...
- 让我们创建屏幕- Android UI布局和控件
下载LifeCycleTest.zip - 278.9 KB 下载ViewAndLayoutLessons_-_Base.zip - 1.2 MB 下载ViewAndLayoutLessons_-_C ...
- Android入门(四)UI-创建自定义控件
原文链接:http://www.orlion.ga/441/ 一.引入布局 iphone应用顶部会有一个标题栏,我们可以模仿着做一个,但是如果我们的程序中很多个活动都需要这样的标题栏,如果 每一个活动 ...
- Android群英传笔记——第三章:Android控件架构与自定义控件讲解
Android群英传笔记--第三章:Android控件架构与自定义控件讲解 真的很久没有更新博客了,三四天了吧,搬家干嘛的,心累,事件又很紧,抽时间把第三章大致的看完了,当然,我还是有一点View的基 ...
- Andriod - 创建自定义控件
控件和布局的继承结构: 可以看到,我们所用的所有控件都是直接或间接继承自 View的,所用的所有布局都是直接或间接继承自 ViewGroup 的.View 是 Android 中一种最基本的 UI 组 ...
随机推荐
- JavaScript中的类(Class)
基本上,ES6的class可以看作只是一个语法糖,它的绝大部分功能,ES5都可以做到的,新的class写法是让对象原型的写法更加清晰,更像面向对象编程的语法而已. ES5生成例对象传统方法是通过构造函 ...
- python基于opencv实现人脸定位
import cv2 # 读取图片 img = cv2.imread("image.jpg") # 加载模型,模型可以从https://github.com/opencv/open ...
- Java HashMap实现原理分析
参考链接:https://www.cnblogs.com/xiarongjin/p/8310011.html 1. HashMap的数据结构 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是 ...
- 015_linuxC++之_覆写
34.类成员函数的重载.覆盖和隐藏区别?答案:a.成员函数被重载的特征:(1)相同的范围(在同一个类中):(2)函数名字相同:(3)参数不同:(4)virtual 关键字可有可无.b.覆盖是指派生类函 ...
- linux下pyenv的安装和使用
一:pyenv介绍 项目地址:https://github.com/pyenv/pyenv pyenv lets you easily switch between multiple vers ...
- istio 安装与bookinfo示例运行
目的 本文旨在帮助想了解istio安装和运行bookinfo示例的同学快速入门 前置准备 安装k8s和helm 1.k8s安装 修改主机名 hostnamectl set-hostname k8s-m ...
- OI程序常见的设计陷阱
宏定义的问题 有时候为了方便,我会大量使用宏定义.但是最近我发现下面这两个宏定义老是出问题: #define SET(x,a) memset(x,a,sizeof(x)) inline void wo ...
- synchronized的对象锁和类锁
概念 synchronized 是 Java 中的关键字,是利用锁的机制来实现同步的. 锁机制有如下两种特性: 互斥性:即在同一时间只允许一个线程持有某个对象锁,通过这种特性来实现多线程中的协调机制, ...
- axios中出现两次请求,OPTIONS请求和GET请求
在项目中发现ajax中出现两次请求,OPTIONS请求和GET请求 查看到浏览器NetWork有两次请求,请求url一样: 查找原因是浏览器对简单跨域请求和复杂跨域请求的处理区别. XMLHttpRe ...
- FutureTask用法及解析
1 FutureTask概念 FutureTask一个可取消的异步计算,FutureTask 实现了Future的基本方法,提空 start cancel 操作,可以查询计算是否已经完成,并且可以获取 ...