Android中LinearLayout布局的常用属性总结
作者:chris930921 发布时间:2023-11-23 17:09:37
标签:Android,LinearLayout
基本属性要求
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
android:orientation
决定是水平排列或是垂直排列
vertical 垂直排列
horizontal 水平排列
垂直排列 Button
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
</LinearLayout>
水平排列 Button
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
</LinearLayout>
重心设定
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="left">
</LinearLayout>
android:gravity
设定框架的内容的放置方向
center 水平垂直皆置中
center_vertical 垂直置中
center_horizontal 水平置中
top 置顶
left 置左
bottom 置底
right 置右
水平、垂直置中
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
</LinearLayout>
透过 OR 运算子组合重心
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="top|right">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom|left">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical|center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
</LinearLayout>
比例分配
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_weight="1"/>
</LinearLayout>
android:layout_weight
子元件或子框架的比重。
LinearLayout 下的子元件或子框架,才能设定这项属性。
等比例分配
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_weight="1"/>
</LinearLayout>
比重都是 1,所以大小相同。
非等比例分配
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_weight=".10"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_weight=".20"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_weight=".70"/>
</LinearLayout>
.10 代表 0.10
.20 代表 0.20
.70 代表 0.70
合起来刚好是 1 ,作 100% 分配。


猜你喜欢
- 前言插入排序狭义上指的是简单插入排序(选择集合,比较大小,插入元素),广义上还应该包括希尔排序(分治思想)及其两种实现方式,最激动人心的是
- 大家好,记得上次我带着大家一起实现了一个类似与淘宝客户端中带有的图片滚动播放器的效果,但是在做完了之后,发现忘了加入图片自动播放的功能(或许
- 描述:由于产品需求,要求含有EditText的界面全屏显示,最好的解决方式是使用AndroidBug5497Workaround.assis
- 今天使用mybatis-plus自动填充插入和更新时间有8小时时差后来发现只需要修改一下mybaits连接的url即可原先我是用的datas
- 0.引言死信队列是消息队列中非常重要的概念,同时我们需要业务场景中都需要延迟发送的概念,比如12306中的30分钟后未支付订单取消。那么本期
- 这几年一直在做手机上和电视盒的App,几乎没有考虑过横竖屏切换的问题。电视盒好说,横屏不变,你要是给它设计个竖屏人家也没机会使;而手机上的应
- 稀疏数组:当一个二维数组中大部份的值为0,或者为同一值的时候,可以用稀疏数组来保存实现思路:记录二维数组有多少行多少列、多少个不同的值把不同
- 发生服务器 500 异常,如果默认方式处理,则是将异常捕获之后跳到 Tomcat 缺省的异常页面,如下图所示。不论哪个网站都是一样的,所以为
- Android Java 如何调用自己的 C++ 的类库下面以 Java 调用 C++ 的加法运算函数为例,做简单说明。(使用 Androi
- 这两天遇到一个服务假死的问题,具体现象就是服务不再接收任何请求,客户端会抛出Broken Pipe。检查系统状态执行top,发现CPU和内存
- Apache Thrift 是 Facebook 实现的一种高效的、支持多种编程语言的远程服务调用的框架。和其它RPC框架相比,它主要具有如
- 项目中遇到了下载文件文件名是中文而且还有空格如果不对连接进行处理下载就会报错要想解决这个问题只需对你的url 进行编码然后替换空格用编码表示
- 数组:是一组相关变量的集合数组是一组相关数据的集合,一个数组实际上就是一连串的变量,数组按照使用可以分为一维数组、二维数组、多维数组数据的有
- 一、使用策略枚举来优化if-else看到网上蛮多人推荐使用策略模式来优化if-else,但我总觉得,搞一堆策略类来优化大批量if-else,
- 在《Android Handler之消息循环的深入解析》中谈到了Handler是用于操作线程内部的消息队列,所以Handler可以用来线程间
- 当一个列表项目很多,并且每个项目可以进入到其它Activity或者Fragment时,保存之前列表的位置是一个比较不错的功能,今天研究了一下
- 首先定义一个加在方法上的注解import java.lang.annotation.*;/** * 开启自动参数填充 */@Retentio
- Java接口(interface)的概念及使用在抽象类中,可以包含一个或多个抽象方法;但在接口(interface)中,所有的方法必须都是抽
- Form1主窗体:public delegate void SetVisiableHandler();//定义委托类型private voi
- 前言今天在逛某知名论坛的时候,看到一篇"请不要使用包装类型,避免造成性能损失"的文章。一下子就吸引了我的注意。大意就是,