android 常用布局

LinearLayout(线性布局)       
线性的 垂直的 水平的
RelativeLaytout(相对布局)    
最灵活的
TableLayout(表格布局)     
使用GridView代替
AbsoluteLayout(绝对布局)    
最好不要使用 绝对坐标
Framelayout(帧布局)      
布局叠加时使用 比如视频缓冲的环形滚动条
使用频次
Absolute<Table<Frame<Linear<Relative
android布局原则
(1)尽量多的使用线性布局和相对布局不要使用绝对布局
(2)在布局层次一样的情况下,建议使用线性布局代替相对布局因为线性布局性能稍高一点
(3)将可复用的组件抽取出来,并通过include标签使用
(4)使用viewstub标签加载一些不常用的布局 (暂时不使用的)
(5)使用merge标签减少标签的嵌套层次

<include/>的使用
作用:将公用的组件抽取出来单独一个xml文件,然后使用include标签导入公共布局
效果:提高ui的制作和复用效率
通过findViewById 也可以找到view 因为通过include实际上是将自布局直接包含进公共布局当中 1,子布局title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="30dp"
android:background="#243821">
</LinearLayout>

  2,主布局main.xml

<LinearLayout
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical">
<include layout="@layout/title"
></include>
</LinearLayout>

使用merge合并ui布局
作用:合并ui布局,使用该布局能降低ui布局的嵌套层次
场景1;布局根节点是FrameLayout且不需要设置backgroud和padding等属性,可以用merge代替
场景2:某布局作为自布局被其他布局include时,使用merge当做该布局的顶接点,这样再被引入时,
顶接点会自动被忽略
1,子布局progress.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"> <ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progress"
android:layout_gravity="center"/>
</merge>

  2,主布局main.xml

<LinearLayout
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical">
<include layout="@layout/title"
></include> <FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <include layout="@layout/progress"/>
</FrameLayout>
</LinearLayout>

使用viewstub惰性加载
作用:viewstub标签同incude标签一样可以用来引入一个外部的布局,不同的是viewstub引入的布局默认不会扩张,
既不会占用显示也不会占用位置,从而在解析laytou时,节省cpu和内存
1,子布局common.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView
android:id="@+id/text"
android:text="viewstub"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

  2,主布局main.xml

<LinearLayout
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical">
<include layout="@layout/title"
></include> <FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <include layout="@layout/progress"/>
</FrameLayout>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="按钮"
/>
<ViewStub
android:id="@+id/viewStub"
android:layout="@layout/common"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

  3,MainActivity

public class MainActivity extends Activity {

    private Button btn ;
private ViewStub viewStub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btn);
viewStub = (ViewStub)findViewById(R.id.viewStub);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewStub.inflate();
}
});
}
}
 
  
 
 

android 学习Layout布局的使用的更多相关文章

  1. 14.Android之Layout布局学习

    Android布局主要有5种,接下来学习总结下. 1) 最常见的线性布局 LinearLayout 线性布局是Android布局中最简单的布局,也是最常用,最实用的布局. android:orient ...

  2. android 学习 之 布局(上)

    学习安卓布局前,先了解三个属性值: 1.fill_parent: 设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间 2.match_parent: And ...

  3. Android学习5—布局简介

    Android界面的布局主要有四种,分别为RelativeLayout.LinearLayout.TableLayout.FrameLayout,接下来分别介绍这些布局如何使用(为了简单起见,接下来的 ...

  4. android开发学习---layout布局、显示单位和如何进行单元测试

    一.五大布局(layout) android中的用五大布局:LinearLayout (线性布局).AbsoluteLayout(绝对布局).RelativeLayout(相对布局).TableLay ...

  5. Android学习----五大布局

    1.LinearLayout 线性布局 android:orientation="horizontal" 制定线性布局的排列方式 水平 horizontal 垂直 vertical ...

  6. android 给layout布局添加点击事件

    <方法一> 1,在代码中加入如下红色代码,不然会被包含在其中的控件把焦点抢占,此时子控件无需设置clickable和focuseable <RelativeLayout        ...

  7. Android学习——LinearLayout布局实现居中、左对齐、右对齐

    android:orientation="vertical"表示该布局下的元素垂直排列: 在整体垂直排列的基础上想要实现内部水平排列,则在整体LinearLayout布局下再创建一 ...

  8. android 学习 之 布局(下)LinearLayout,RelativeLayout,TableLayout,FrameLayout

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  9. 工作不久的安卓开发者,他们是这样规划自己的Android学习路线

    Android开发工作者工作不久的时候,会有一段迷茫期,觉得自己应该再学一点,却不知道从何学起,该怎样规划自己的学习路线呢?今天,我给大家梳理一下Android基础,就像建造房屋一样,要建造一座宏伟的 ...

随机推荐

  1. ArrayList原理解析

    简介 ArrayList就是动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了动态的增加和减少元素,实现了ICollection和IList接口,灵活的设置数组的大小等好处 有图有码 图 ...

  2. JS实现为控件添加倒计时功能

    一.概述 在有些报表需求中,需要为控件添加倒计时功能,限制到某一个时间点后能进行一项操作或不能进行某项操作,比如查询,导出功能等等,又需要人性化地显示还有多少时间,即倒计时功能,比如下图中我们限制这个 ...

  3. centos7架设vsftpd服务

    网络控制相关命令: systemctl status network 网络状态 systemctl restart network 网络重启 查看网络状态: nmcli connection show ...

  4. 钉钉 机器人接入 自定义webhook

    钉钉出了个webhook机器人接入,自定义的机器人支持随时post消息到群里: 昨天就尝试着用C#写了个: 一开始用python写,但是莫名的提示  {"errmsg":" ...

  5. Python之__loader__

    主要引自:http://stackoverflow.com/questions/22185888/pythons-loader-what-is-it What is __loader__? __loa ...

  6. Echarts折线图表断点如何补全

    Echarts折线图如何补全断点以及如何隐藏断点的title 做报表的时候,尤其是做图表的时候时常会碰到某一记录的值中缺少某个时间段(比如月份或季度)的值,导致图表显示残缺不全,for example ...

  7. (转)CentOS 7.0关闭默认防火墙启用iptables防火墙

    场景:在本地虚拟机上使用ftp软件需要进行相应的端口设置,不可避免要访问Cnetos的防火墙,默认firewall操作不方便,所以需要进行相应的替换. 1 配置防火墙,开启80端口.3306端口 1. ...

  8. (转)java提高篇(二)-----理解java的三大特性之继承

    在<Think in java>中有这样一句话:复用代码是Java众多引人注目的功能之一.但要想成为极具革命性的语言,仅仅能够复制代码并对加以改变是不够的,它还必须能够做更多的事情.在这句 ...

  9. MyBatis 详解(一对一,一对多,多对多)

    1.什么是MyBatis? MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且 ...

  10. jsp变量的使用规则

    jsp是一种弱类型的交而不能语音,虽然看似没有像强类型语言那么多的代码规范,但是在实际使用的过程当中依然有不少的问题.下面就简单的梳理一下. 1.首先,jsp是一种弱类型的脚本语言,变量在使用之前无需 ...