1、LinearLayout(线性布局)

  该布局将其中的View子控件按照水平或者垂直方向排列。但是需要注意不管是水平还是竖直,对应的每一行或列都只能放一个控件。

线性布局两种排法:

  从左到右:android:orientation="horizontal"

  从上到下:android:orientation="vertical"

效果如下:

 
<?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="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:orientation="horizontal">
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#b2dfdb" />
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#80cbc4" />
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#4db6ac" />
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#26a69a" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#b2dfdb" />
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#80cbc4" />
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#4db6ac" />
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#26a69a" />
</LinearLayout>
</LinearLayout>

  一个竖向的大LinearLayout嵌套着两个小LinearLayout,第一个小LinearLayout为横向,第二个小LinearLayout为竖向。

  2、RelativeLayout布局

  参考其他控件进行布局,默认为父控件。

  有三种类型的属性:

    属性值是true或false

      android:layout_centerHrizontal 水平居中
      android:layout_centerVertical 垂直居中
      android:layout_centerInparent 相对于父元素完全居中。
      android:layout_alignParentBottom  位于父元素的下边缘
      android:layout_alignParentTop  位于父元素的上边缘
      android:layout_alignParentLeft 位于父元素的左边缘
      android:layout_alignParentRight 位于父元素的右边缘

    属性值是“@id/*”

      android:layout_below 在某元素的下方
      android:layout_above 在某元素的上方
      andorid:layout_toRightOf 在某元素的右方
      android:layout_toLeftOf 在某元素的左方
      android:layout_alignBottom 和某元素下方对齐
      android:layout_alignTop 和某元素上方对齐
      android:layout_alignRight 和某元素右方对齐
      android:layout_alignLeft 和某元素左方对齐

    属性值是数值

      android:layout_marginLeft 离某元素左边缘的距离
      android:layout_marginRight 离某元素右边缘的距离
      android:layout_marginTop 离某元素上边缘的距离
      android:layout_marginBottom 离某元素下边缘的距离

  注意:

    如果没有定义左右,那么默认在左边,如果没有定义上下,默认在上边。
    相同位置,新定义的元素会覆盖旧的元素。
    只定义在父元素的下部,左右没有定义,默认在左边。
    android:layout_below,在某元素的下部并不意味着就一定是紧随某元素,只是在下部的默认位置。
  3、MyLayout布局(自定义ViewGroup)

  自定义布局主要是重写两个方法:

    onMeasure()写自定义容器的大小。

    onLayout()写子元素的布局。

    3.1、onMeasure()

 @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
/**
* 获得此ViewGroup上级容器为其推荐的宽和高,以及计算模式
*/
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); // 计算出所有的childView的宽和高
measureChildren(widthMeasureSpec, heightMeasureSpec);
/**
* width和height是当wrap_content时使用的属性。
*/
int width = 0;
int height = 0;
int cCount = getChildCount();
int cWidth = 0;
int cHeight = 0;
/**
* 在这里计算当wrap_content时,布局的大小。
*/
for (int i = 0; i < cCount; i++) {
View childView = getChildAt(i);
cWidth = childView.getMeasuredWidth();
cHeight = childView.getMeasuredHeight();
width += cWidth;
height += cHeight;
}
/**
* 如果是wrap_content设置为我们计算的值
* 否则:直接设置为父容器计算的值
*/
setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? sizeWidth
: width, (heightMode == MeasureSpec.EXACTLY) ? sizeHeight
: height); }

  布局计算模式,即最后的EXACTLY。共有三种计算模式:

    MeasureSpec.EXACTLY:精确尺寸,相当于具体数值和match_parent.

    MeasureSpec.AT_MOST:最大尺寸,相当于warp_content.

    MeasureSpec.UNSPECIFIED:未指定尺寸,这种情况不多,一般用于AdapterView.

    3.2、onLayout()

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int cCount = getChildCount(); /**
* 遍历所有childView根据其宽和高,以及margin进行布局
*/
for (int i = 0; i < cCount; i++) {
View childView = getChildAt(i);
r = l + childView.getMeasuredWidth();
b = t + childView.getMeasuredHeight();
childView.layout(l, t, r, b);
l += childView.getMeasuredWidth();
t += childView.getMeasuredHeight();
}
}

  这个方法的作用是设置摆放子元素的位置。其中onLayout()传入的l、t、分别是对应子元素左上角的left,top坐标。r、b分别是对应子元素右下角的right,bottom坐标。

  可以使用childview.getMeasureWidth()和childView.getMeasureHeight()得到子元素的宽和高。

  xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<com.example.layoutdemo.MyLayout.MyLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
> <TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#b2dfdb" /> <TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#80cbc4" /> <TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#4db6ac" /> <TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#26a69a" /> </com.example.layoutdemo.MyLayout.MyLayout>

  4、FrameLayout布局

  帧布局,这个布局的特点是从左上角开始,后面的会覆盖前面的控件。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="100dp"
android:textColor="#9c27b0"
android:text="第一层"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="80dp"
android:textColor="#e91e63"
android:text="第二层"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60dp"
android:textColor="#e51c23"
android:text="第三层"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:textColor="#5677fc"
android:text="第四层"/>
</FrameLayout>

效果图如下:

  

5、TableLayout布局

  表格布局,应遵循以下布局结构。

<TableLayout>
<TableRow>
<!-在这里填充第一行的元素->
</TableRow>
<TableRow>
<!-在这里填充第二行的元素->
</TableRow>
</TableLayout>

  几个重要属性:

    写在TableLayout中的属性

      android:stretchColumns  设置第几列为伸展(0表示第一列)
      android:shrinkColumns   设置第几列为收缩
      android:collapseColumns 设置第几列为隐藏
    写在TableRow里的控件里的属性
      android:layout_column  设置控件在第几列
      android:layout_span  设置控件能跨多少列

  代码如下:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:collapseColumns="2"
android:shrinkColumns="1"
android:stretchColumns="0"> <TableRow
>
<TextView android:text="我是伸展的第一列" /> <TextView android:text="我是收缩的第二列" /> <TextView android:text="我被隐藏了" />
</TableRow> <TableRow>
<TextView android:text="我可以伸展的很长很长很长长" /> <TextView android:text="我可以收缩,我可以变的很深很深很深" /> <TextView android:text="我被隐藏了T_T" />
</TableRow> <TableRow>
<TextView
android:layout_column="1"
android:text="我要在第2列" />
</TableRow> <TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_span="2"
android:text="我要 跨 两 列" />
</TableRow>
</TableLayout>

  效果图如下:

参考文章:https://blog.csdn.net/u013254061/article/details/52512146

Android Studio局部管理器的更多相关文章

  1. Android TelephonyManager电话管理器

    今天介绍一下Android的电话管理器--TelephonyManager,TelephonyManager管理手机通话状态.电话网络信息的服务类,获取TelephonyManager: Teleph ...

  2. android的布局管理器

    理论上通过setContentView(view)能够把一个view设置到activity中,但当你有很多个view控件的时候,就需要用android的布局管理器来管理view控件了. android ...

  3. Android之声音管理器《AudioManager》的使用以及音量控制

    以下为网上下载然后拼接-- Android声音管理AudioManager使用 手机都有声音模式,声音.静音还有震动,甚至震动加声音兼备,这些都是手机的基本功能.在Android手机中,我们同样可以通 ...

  4. 一步一步学android之布局管理器——LinearLayout

    线性布局是最基本的一种布局,在基本控件篇幅中用到的都是LinearLayout,线性布局有两种方式,前面也有用到,一种是垂直的(vertical),一种是水平的(horizontal).我们同样来看下 ...

  5. Visual Studio 代码管理器svn插件下载

    环境:Visual Studio 2010 Visual Studio的svn插件叫做VisualSVN,可自行到VisualSVN官网上下载相应版本,也可以通过vs中找到相关插件. ps:vs其他的 ...

  6. Android课程---布局管理器中的线性布局

    线性布局实例: <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:andro ...

  7. 一步一步学android之布局管理器——RelativeLayout

    今天开始学习RelativeLayout(相对布局),相对布局在平时布局的时候用的较多,因为Android适配方面的原因.相对布局可以控制组件摆放的位置(放在任一组件的上下左右等位置),下面来看看类的 ...

  8. android studio 统一管理版本号配置

    1.在android 的根目录新建一个versions.gradle 2.在这里面声明 各个第三方库的版本,写法有两种,第一种,写ext 扩展, 引用的时候, 第二种: 然后在project级的bui ...

  9. Android课程---布局管理器

随机推荐

  1. java 面向对象(三十四):泛型三 自定义泛型类、泛型接口、泛型方法

    1.举例: [Order.java] public class Order<T> { String orderName; int orderId; //类的内部结构就可以使用类的泛型 T ...

  2. celery 基础教程(五):守护进程

    一 守护进程方式启动 https://blog.csdn.net/p571912102/article/details/82735052 文件目录如下 . ├── config.py ├── main ...

  3. 机器学习实战基础(十):sklearn中的数据预处理和特征工程(三) 数据预处理 Preprocessing & Impute 之 缺失值

    缺失值 机器学习和数据挖掘中所使用的数据,永远不可能是完美的.很多特征,对于分析和建模来说意义非凡,但对于实际收集数据的人却不是如此,因此数据挖掘之中,常常会有重要的字段缺失值很多,但又不能舍弃字段的 ...

  4. 数据可视化之PowerQuery篇(十二)客户购买频次分布

    https://zhuanlan.zhihu.com/p/100070260 商业数据分析通常都可以简化为对数据进行筛选.分组.汇总的过程,本文通过一个实例来看看PowerBI是如何快速完成整个过程的 ...

  5. UML学习笔记—基本概念和初始阶段

    chpater1 1.什么是分析和设计 分析:对问题和需求的调查研究 设计:满足需求的概念上的解决方案 做正确的事(分析)和正确地做事(设计) 2.什么是Object-Oriented-Analysi ...

  6. k8s极简史:K8s多集群技术发展的历史、现状与未来

    引子 随着云原生技术的普及,越来越多的企业使用Kubernetes来管理应用,并且集群规模也呈爆发式增长,企业也亟需应对随集群规模增长而带来的各种挑战.同时,为了更好地提供高可用.弹性伸缩的应用,企业 ...

  7. Sharding-Proxy的基本功能使用

    Sharding-Proxy是一个分布式数据库中间件,定位为透明化的数据库代理端.作为开发人员可以完全把它当成数据库,而它具体的分片规则在Sharding-Proxy中配置.它的整体架构图如下: 在架 ...

  8. Linux/Docker 中使用 System.Drawing.Common 踩坑小计

    前言 在项目迁移到 .net core 上面后,我们可以使用 System.Drawing.Common 组件来操作 Image,Bitmap 类型,实现生成验证码.二维码,图片操作等功能.Syste ...

  9. 瀑布流的实现纯CSS实现Jquery实现

    瀑布流的实现 注:本文部分图片自百度下载,如有侵权,联系删图. 首先,选择几张图片布局到HTML内容中.HTML如下所示. <div class="wrapper"> ...

  10. Linux 终端最全推荐(建议收藏)

    本文来自网络整理,如有侵权,则可删除. 如果你跟我一样,整天要花大量的时间使用Linux命令行,而且正在寻找一些可替代系统自带的老旧且乏味的终端软件,那你真是找对了文章.我这里搜集了一些非常有趣的终端 ...