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. Image和字节数组互转

    using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; ...

  2. MOF编译器无法连接VMI服务器。原因可能是语义错误的解决方案

    安装数据库时报错. 我这个是因为安装SQL SERVER时,没有卸载vs. 一般解决方法: WIN 7安装VS和SQL SERVER的顺序应该是先安装SQL SERVER 然后安装VS,当要重装SQL ...

  3. 学习java 的经验

    一.学习java首先应该对他有个全局的看法 ,他由几部分组成 1.java 的基础语法 2.使用Swing 来做桌面应用,可做嵌入式开发. 3.JDBC数据库的链接 4.网络编程,主要是socket编 ...

  4. VB6之图像灰度与二值化

    老代码备忘,我对图像处理不是太懂. 注:部分代码引援自网上,话说我到底自己写过什么代码... Private Declare Function GetBitmapBits Lib "gdi3 ...

  5. frames.contentWindow.document InvalidCastException 转换错误异常。

    http://bbs.csdn.net/topics/210027068   和 https://bytes.com/topic/c-sharp/answers/248557-threading-pr ...

  6. Chromium模块和进程模型

    i. Chromium基本模块 Chromium各模块层级图a) Chromium主要包括如下模块: WebKit:  Safari和Chromium,以及任何其他基于WebKit内核的浏览器所共享的 ...

  7. js实现存储对象的数据结构hashTable和list

    以下代码是typescript语言来写的,其实和es6面向对象的写法基本一致.大家阅读后都明白这些方法的作用. hash hash结构用于处理和表现类似key/value的键值对,其中key通常可用来 ...

  8. Python项目实战:福布斯系列之数据采集

    1 数据采集概述 开始一个数据分析项目,首先需要做的就是get到原始数据,获得原始数据的方法有多种途径.比如: 获取数据集(dataset)文件 使用爬虫采集数据 直接获得excel.csv及其他数据 ...

  9. Python面向对象编程(二)

    1.继承与派生 上文我们已经说过,Python中一切皆对象.我们从对象中抽取了共同特征和技能,得到了类的概念.类与类之间也有共同特征,我们可以从有共同特征和技能的类中提取共同的技能和特征,叫做父类. ...

  10. (转)CentOS6.5下Redis安装与配置

    场景:项目开发中需要用到redis,之前自己对于缓存这块一直不是很理解,所以一直有从头做起的想法. 本文详细介绍redis单机单实例安装与配置,服务及开机自启动.如有不对的地方,欢迎大家拍砖o(∩_∩ ...