Android 引入布局
一、仅引入布局文件
1、 新建一个xml布局文件title.xml,写入自定义布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:background="#B06DFA"android:layout_width="match_parent"android:layout_height="wrap_content"><ImageButtonandroid:id="@+id/butBack"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/back"></ImageButton><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="POWER MANAGER"android:layout_gravity ="center"android:gravity="left"android:layout_weight="2"android:layout_margin="5dp"></TextView><ImageButtonandroid:id="@+id/butInfo"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/info"></ImageButton></LinearLayout>
2、在我们要引入title的布局文件中包含title,使用include
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><include layout="@layout/title"></include></androidx.constraintlayout.widget.ConstraintLayout>
这一种方法仅仅把ui引入了,但是ui里控件的功能还需要引入一次写一次,第二种方法把这个功能也封装起来。
二、把控件功能也封装起来
1、还是要写布局文件,同方法一的1
2、新建java类TitleLayout 继承 LinearLayout
public class TitleLayout extends LinearLayout {public TitleLayout(Context contex, AttributeSet attrs){super(contex,attrs);LayoutInflater.from(contex).inflate(R.layout.title,this);ImageButton butBack=findViewById(R.id.butBack); //返回图标按钮ImageButton butInfo=findViewById(R.id.butInfo); //详细信息按钮butBack.setBackgroundResource(0); //隐藏背景butInfo.setBackgroundResource(0);//返回按钮butBack.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(contex, "click:back", Toast.LENGTH_SHORT).show();}});//详细信息按钮butInfo.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(contex, "click:info", Toast.LENGTH_SHORT).show();}});}
}
3、在我们要引入title的布局文件中,像普通控件一样,不能再用include
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.example.test.TitleLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"></com.example.test.TitleLayout></androidx.constraintlayout.widget.ConstraintLayout>
Android 引入布局相关推荐
- Android开发——布局性能优化的一些技巧(一)
0. 前言 上一篇我们分析了为什么LinearLayout会比RelativeLayout性能更高,意义在于分析了这两种布局的实现源码,算是对一个小结论的证明过程,但是对布局性能的优化效果,对这两种布 ...
- autojs遍历当前页面所有控件_自定义控件(引入布局)
系统给我们提供了许多功能强大的控件,我们在需要时可以直接在布局中添加使用,但是有时候我们程序想要实现的功能往往因人而异,如:我们的程序需要在内个Activity的标题栏左侧有一个返回按钮(类似于iPh ...
- android 首页布局变换,Android XML布局与View之间的转换
Android的布局方式有两种,一种是通过xml布局,一种是通过java代码布局,两种布局方式各有各的好处,当然也可以相互混合使用.很多人都习惯用xml布局,那xml布局是如何转换成view的呢?本文 ...
- Android百分比布局支持和垂直TextView
In this tutorial, we'll discuss and implement Android Percent Layout Support Library. Furthermore, w ...
- android折叠布局,Android中FoldingLayout折叠布局的用法及实战全攻略
一.概述无意中翻到的FoldingLayout的介绍的博客,以及github地址.感觉很nice呀,于是花了点时间研究以及编写,本篇博客将带大家从最基本的原理分析,一步一步的实现我们的FoldingL ...
- android如何看百分比版本,【JAVA】Android百分比布局
开发我人生第一个app的时候,我在想有没有这么一种布局能适应任何手机的分辨率,今天让我们一起来看看Android 百分比布局PercentRelativeLayout. 使用这种布局首先我们要在gra ...
- 浅谈Android五大布局——LinearLayout、FrameLayout和AbsoulteLa
为什么80%的码农都做不了架构师?>>> Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了 ...
- android源代码居中字体,Android (布局优化) TextView实现drawable图标大小 位置与第一行文本居中...
先看大众点评的购买须知 大众.png 如上图,需求在每条提示语句前加一个小圆点,我刚看到需求就想到用 android:drawableLeft 来做,可做完发现:当TextView内容为单行的时候是没 ...
- Android 线性布局(LinearLayout)相关官方文档 - 布局參数部分
Android 线性布局(LinearLayout)相关官方文档 - 布局參数部分 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商 ...
最新文章
- 架构与思维:系统容量设计
- digitalocean添加ssh_keys
- MapXtreme 操作地图时出现调用目标发生异常的解决方法--地图状态保存
- ElasticSearch 2.2 升级 6.2.4
- 进一步考察与UI相关的安全漏洞-上
- HDU 1525 - Euclid's Game ( 博弈 )
- python计时器程序设计总框图_怎么用python编写计时器?这个编程的方法你一定不要错过...
- 代码居中对齐_HTML span标签如何居中和右对齐?这里有HTML span标签的样式解析
- 动态加载并获取usercontrol生成的html
- UEFI win7系统的安装
- java List 排序 Collections.sort()
- Magento 自定义EMS FEDEX DHL UPS扩展 第三章
- Hive表生命周期管理
- win10专业版激活方法
- 什么是元数据?为何需要元数据?
- ARKit之路-运动跟踪原理
- DDD系列第五讲:聊聊如何避免写流水账代码
- 微信公众平台推数据统计功能 商业价值可量化
- C++实现字符串匹配KMP算法
- 用python发微博