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% 分配。
0
投稿
猜你喜欢
- 为什么要在控制台输出 SQL 呢?当然是为了开发调试的时候方便了。如果一个 数据库相关的操作出现了问题,我们可以根据输出的SQL语句快速排查
- Java doGet, doPost方法和文件上传index.html<!DOCTYPE html><html lang=
- 本文实例讲述了Android基于DialogFragment创建对话框的方法。分享给大家供大家参考,具体如下:/** * 使用DialogF
- 1、背景我有一堆学生数据,其中湖北省的学生需要排在所有数据的最前面。其余省正序排序,对于同一个省的数据,按照年龄倒序排序。2、分析对于上方的
- 本文实例为大家分享了Android实现View滑动的具体方法,供大家参考,具体内容如下1.View的滑动简介View的滑动是Android实
- 需求基于MTK8163 8.1平台定制导航栏部分,在左边增加音量减,右边增加音量加思路需求开始做之前,一定要研读SystemUI Navig
- 这篇文章主要介绍了Mybatis 缓存原理及失效情况解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 公司的以前的项目,看到使用了这个Android自带的倒计时控件Chronometer,现在整合了一下先看看效果:<Chronomete
- 前言目前,企业项目的开发过程中,往往会使用配置文件来做一些配置项来实现项目部署的灵活性,避免硬编码的方式在环境变化时需要对代码进行重新编译。
- 在网上也没有找到好的解决方案,于是自己研究了下给解决了,分享给大家,希望对大家能有所帮助。一、异常信息这种情况是因为FTP设置的默认目录引发
- 实例如下://首先要添加 System.ServiceProcess.dll 引用 ServiceController sc
- Volley 是一个 HTTP 库,它能够帮助 Android app 更方便地执行网络操作,最重要的是,它更快速高效。我们可以通过开源的
- 1.类加载机制Java中的源码.java后缀文件会在运行前被编译成.class后缀文件,文件内的字节码的本质就是一个字节数组 ,它有特定的复
- 项目中常用到的圆形进度条有好多个,从网上搜到的自定义进度条多是封装的比较好的代码,但是不利于初学者,现在本博客就教给大家如何一步步实现自定义
- using System;using System.Collections.Generic;using System.ComponentMo
- 本文实例为大家分享了java生成登录随机验证码的具体代码,供大家参考,具体内容如下验证码工具类: VerifyUtil.javaimport
- 本文实例为大家分享了Android实现连连看游戏的具体代码,供大家参考,具体内容如下本人用 android studio 实现的源码主活动
- 背景在工作中,遇到这样的场景:有个es索引构建服务,需要从各个业务服务获取索引的信息,从而构建索引,业务服务都实现同一个接口&mda
- 找到framework/base/core/res/res/values/dimens.xml,在其中把Navigation的配置改成0 &
- 本文实例为大家分享了C#实现QQ聊天窗口的具体代码,供大家参考,具体内容如下分析需要两个TextBox,一个用于显示消息,一个用于编辑消息需