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. ExtJs的Ext.Ajax.request实现waitMsg等待提示效果

    一.  fp.form.submit 有waitMsg 属性来设置等待效果,如下.但是对于Ext.Ajax.request来说 waitMsg 并不起作用.                     f ...

  2. Spring源码:IOC原理解析(二)

    版权声明:本文为博主原创文章,转载请注明出处,欢迎交流学习! 接着上一章节的内容,我们来分析当new一个FileSystemXmlApplicationContext对象的时候,spring到底做了那 ...

  3. C/C++中对链表操作的理解&&实例分析

    链表概述 链表是一种常见的重要的数据结构.它是动态地进行存储分配的一种结构.它可以根据需要开辟内存单元.链表有一个“头指针”变量,以head表示,它存放一个地址.该地址指向一个元素.链表中每一个元素称 ...

  4. jpg、jpeg、png... 的区别

    对于做设计这一行的人来说,这几个图片格式是最常用的,也是最常见的,几乎每一天都要与他们打交道. 刚刚入门的新人通常不知道在什么地方如何使用他们或者说如何更有效的使用他们. 那他们到底是有什么区别?(一 ...

  5. C# 哈希表(Hashtable)用法笔记

    一.什么是Hashtable? Hashtable 类代表了一系列基于键的哈希代码组织起来的键/值对.它使用键来访问集合中的元素. 当您使用键访问元素时,则使用哈希表,而且您可以识别一个有用的键值.哈 ...

  6. JavaScript+canvas 绘制多边形

    效果图: <body> <canvas id="square" width="500"></canvas> <canv ...

  7. Oracle之plsql快速入门

    打开系统输出 set serveroutput on; 只需要打开一次**书写格式 以斜杠/号 结束(基本结构) --declare --语句后面必须以;号结束 declare --用来区分变量名和表 ...

  8. Spark的误解-不仅spark是内存计算,hadoop也是内存计算

    市面上有一些初学者的误解,他们拿spark和hadoop比较时就会说,Spark是内存计算,内存计算是spark的特性.请问在计算机领域,mysql,redis,ssh框架等等他们不是内存计算吗?依据 ...

  9. Ubuntu14.04设置开机自启动程序

    启动应用程序可以帮助我们选择开机启动项.但是在Ubuntu14.04通过Dash输入startup 找不到启动应用程序了,可以通过在控制台输入以下内容: gnome-session-propertie ...

  10. 迭代器 Iterator

    迭代器 Iterator 2016-5-7 可以这样说,迭代器统一了对容器的访问方式. 考虑这样的情景:原本是对着List编码,但是后来发现需要把相同的代码用于Set.我们需要一种不关心容器类型 而能 ...