关于Android布局优化的代码使用
1. include标签:
include标签的作用是在一个布局文件中导入另一个布局文件。在开发中经常会有多个页面同时拥有一部分相同的布局,这个时候如果每个布局都把那个部分的代码写一遍就会使得代码重复,浪费了资源。使用include就可以简单的导入相同部分的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="欢迎您使用这款app"
android:textSize="16dp"
android:gravity="center"/>
</LinearLayout>
以上代码是一个多页面通用的布局,给它命名为include.xml,接下来在主页面activity_main.xml中引用它:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
> <include layout="@layout/include"/> //使用include引用了子布局 </LinearLayout>
于是在主页面中便看到了include页面中的内容。 2. merge标签:
merge标签的作用是减少布局嵌套的层次。比如刚才那个例子中,父布局与子布局都是使用的线性布局LinearLayout,这样在代码中
等于重复写了两次的布局声明,产生了不必要的代码,这个时候就可以使用merge标签。
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="欢迎您使用这款app"
android:textSize="16dp"
android:gravity="center"/>
</merge>
以上是修改过后的include.xml。在父布局中导入了这个页面后,这个页面就得到了父布局的LinearLayout布局,所以显示
效果和之前的代码相比没有任何区别,但是精简了代码,节省了系统资源。
3.Viewstub标签:
Viewstub标签的作用是让布局在被加载的时候才会去占用资源,而INVISIBLE状态(不可见状态)下布局不会被绘制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ViewStub
android:id="@+id/include1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout="@layout/include"/>
</LinearLayout>
以上是一个Viewstub的例子,运行程序后会发现Viewstub里的布局include.xml并没有被显示出来,说明Viewstub默认状态
下是会隐藏内容的,然后我们将它改为可视状态:
package com.example.von.include; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.include1).setVisibility(View.VISIBLE); //利用这个方法将include1改为可视状态
}
}
这时再次运行程序,会发现include.xml的内容成功显示了出来。
关于Android布局优化的代码使用的更多相关文章
- 【转】Android布局优化之ViewStub
ViewStub是Android布局优化中一个很不错的标签/控件,直接继承自View.虽然Android开发人员基本上都听说过,但是真正用的可能不多. ViewStub可以理解成一个非常轻量级的Vie ...
- Android布局优化之include、merge、ViewStub的使用
本文针对include.merge.ViewStub三个标签如何在布局复用.有效减少布局层级以及如何可以按需加载三个方面进行介绍的. 复用布局可以帮助我们创建一些可以重复使用的复杂布局.这种方式也意味 ...
- 转:Android布局优化
categories: Android 在Android开发中,我们常用的布局方式主要有LinearLayout.RelativeLayout.FrameLayout等,通过这些布局我们可以实现各种各 ...
- [旧][Android] 布局优化
备注 原发表于2016.05.21,资料已过时,仅作备份,谨慎参考 前言 最近在编写布局时,发现这一块是有很多值得深入学习的地方的.毕竟应用开发,界面展示是十分重要的部分.另外在开发时,为自己的代码做 ...
- Android布局优化:include 、merge、ViewStub的详细总结
版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 本篇博客主要是对上篇博客的补充Android性能优化之UI渲染性能优化, 没有什么新东西,觉得应该是都掌握的玩意,写出来也只是自己做个小小的总结. ...
- [Android]Android布局优化之<include />
转载请标明:转载于http://www.cnblogs.com/Liuyt-61/p/6602891.html -------------------------------------------- ...
- Android成长日记-Android布局优化
Android常用布局 1. LinearLayout(线性布局) 2. RelativeLayout(相对布局) 3. TableLayout(表格布局) 4. AbsoluteLayou(绝对布局 ...
- android 布局优化常用技巧
android对多个模块都要是要的UI逻辑的致辞除了fragment之外,没有别的东西可以支持了, include,merge,viewstub只能支持公用的ui,但是这个通用支持不能包含逻辑(jav ...
- Android布局优化
前言 本篇文章为Android优化的布局部分,该部分应该是Android中很重要的,无论是在自定义控件中,还是在简单的书写布局时,都应该尽量遵循一些优化原则,这样布局的绘制效率才会更高,体验才能更好. ...
随机推荐
- Java Queue之PriorityQueue
PriorityQueue位于Java util包中,观其名字前半部分的单词Priority是优先的意思,实际上这个队列就是具有“优先级”.既然具有优先级的特性,那么就得有个前后排序的“规则”.所以其 ...
- 【easy】27. Remove Element
删除等于n的数,并返回剩余元素个数 Given nums = [3,2,2,3], val = 3, Your function should return length = 2, with the ...
- ES--06
第51.初识搜索引擎_上机动手实战多搜索条件组合查询 课程大纲 GET /website/article/_search{ "query": { "bool": ...
- Copley-STM32串口+CANopen实现双电机力矩同步
原来有个CANopen的主站卡,现在没了,只有单片机,用单片机来制作一个CANopen的主站卡貌似不是很难,但是需要时间.无奈仔细看了一个Copley的说明,决定采用CAN口+串口来实现之前的功能. ...
- git remote: HTTP Basic: Access denied Mac 使用Sourcetree 密码输错 再次输入解决方案
删除下面的key即可
- OrCAD Capture CIS 16.6 将版本16.6的设计文件另存为版本16.2的设计文件
操作系统:Windows 10 x64 工具1:OrCAD Capture CIS 16.6-S062 (v16-6-112FF) 启动OrCAD Capture CIS,打开.dsn设计文件,右击该 ...
- numpy有什么用【老鱼学numpy】
老鱼为了跟上时代潮流,也开始入门人工智能.机器学习了,瞬时觉得自己有点高大上了:). 从机器学习的实用系列出发,我们会以numpy => pandas => scikit-learn =& ...
- 【Android】onNewIntent调用时机
在IntentActivity中重写下列方法:onCreate onStart onRestart onResume onPause onStop onDestroy onNewIntent一.其他应 ...
- 分享关于搭建高性能WEB服务器的一篇文章
这篇文章主要介绍了Centos5.4+Nginx-0.8.50+UWSGI-0.9.6.2+Django-1.2.3搭建高性能WEB服务器的相关资料,需要的朋友可以参考下(http://m.0813s ...
- Android 常用知识点
1.Kotlin 将字节大小转换为KB,MB,GB 并保留两位小数 fun getFileSize(size: Long): String { var GB = 1024 * 1024 * 1024 ...