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. 极简单的方式序列化sqlalchemy结果集为JSON

    继承 json.JSONEncoder 实现一个针对sqlalchemy返回类型的处理方式. sqlalchemy的返回类型有大都有两种,一种是Model对象,一种是Query集合(只查询部分字段). ...

  2. iOS获取ipa素材、提取ipa包资源文件

    当我们看到一款优秀的App时,我们可能对它的一些素材比较感兴趣,或者我们也想仿写一款类似app,那么怎么能获取到它的素材资源文件呢? 下面我以ofo举例: 1.打开iTunes,搜索ofo关键字,选择 ...

  3. biz-NewsService

    package com.pb.news.service; import java.util.List; import com.pb.news.entity.News; public interface ...

  4. ASP.NET Core之跨平台的实时性能监控

    前言 前面我们聊了一下一个应用程序 应该监控的8个关键位置. . 嗯..地址如下: 应用程序的8个关键性能指标以及测量方法 最后卖了个小关子,是关于如何监控ASP.NET Core的. 今天我们就来讲 ...

  5. JavaScript中判断鼠标按键(event.button)

    <div id="test"></div> <script> document.oncontextmenu=function(){ return ...

  6. java三大框架介绍

    常听人提起三大框架,关于三大框架,做了如下了解: 三大框架:Struts+Hibernate+Spring java三大框架主要用来做WEN应用. Struts主要负责表示层的显示 Spring利用它 ...

  7. 20170717_python爬虫之requests+cookie模拟登陆

    在成功登陆之前,失败了十几次.完全找不到是什么原因导致被网站判断cookie是无效的. 直到用了firefox的httpfox之后才发现cookie里还有一个ASP.NET_SessionId 这个字 ...

  8. 动态语言的灵活性是把双刃剑 -- 以Python语言为例

    本文有些零碎,总题来说,包括两个问题:(1)可变对象(最常见的是list dict)被意外修改的问题,(2)对参数(parameter)的检查问题.这两个问题,本质都是因为动态语言(动态类型语言)的特 ...

  9. ARM开发(2)基于STM32的蜂鸣器

    基于STM32的蜂鸣器 一 蜂鸣器原理:  1.1 本实验实现1个蜂鸣器间隔1S鸣叫.  1.2 实验思路:根据电路图原理,给蜂鸣器相关引脚赋予高低电平,实现电路的导通,使蜂鸣器实现鸣叫或不鸣.  1 ...

  10. ionic2+Angular 使用ng2-file-upload 插件上传图片并实现本地预览

    第一步:npm install ng2-file-upload --save 安装 ng2-file-upload 第二步:在需要使用该插件的页面的对应module文件的imports中引入Commo ...