android 布局中 layout_gravity、gravity、orientation、layout_weight

2024-06-21 14:48

本文主要是介绍android 布局中 layout_gravity、gravity、orientation、layout_weight,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

线性布局中,有 4 个及其重要的参数,直接决定元素的布局和位置,这四个参数是

android:layout_gravity ( 是本元素相对于父元素的重力方向 )

android:gravity (是本元素所有子元素的重力方向)

android:orientation (线性布局以列或行来显示内部子元素)

android:layout_weight (线性布局内子元素对未占用空间【水平或垂直】分配权重值,其值越小,权重越大

                            前提是子元素 设置了 android:layout_width = "fill_parent" 属性(水平方向)

                               android:layout_height = "fill_parent" 属性(垂直方向)

 

如果某个子元素的 android:layout_width = "wrap_content"

           android:layout_height =" wrap_content”

android:layout_weight 的设置值 对该方向上空间的分配刚好相反。

下面以一个简单例子来说明这 4个参数

 1 <? xml version = "1.0" encoding = "utf-8" ?>
 2 
 3 < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
 4 
 5                            android:layout_height = "200dp"
 6 
 7                            android:layout_width = "200dp"
 8 
 9                            android:background = "#AABBCC"
10 
11                            android:orientation= "horizontal"
12 
13                            android:layout_gravity= "center" >
14 
15                            < TextView android:text = "ONE"
16 
17                                                android:background = "#aa0000"
18 
19                                                android:layout_height = "wrap_content"
20 
21                                                android:layout_width = "wrap_content"
22 
23                                                android:layout_margin = "1dp" />
24 
25                             < TextView android:text = "TWO"
26 
27                                                android:background = "#aa0000"
28 
29                                                android:layout_height = "wrap_content"
30 
31                                                android:layout_width = "wrap_content"
32 
33                                                android:layout_margin = "1dp" />
34 
35 </ LinearLayout >

说明:在上面的例子中,根布局是LinearLayout, 其包含有2 个TextView 视图,为了对参数 android:layout_gravity有直观的了解,对根布局 LinearLayout 特意加了 3 个参数

android:layout_height = "200dp"

android:layout_width   = "200dp"

android:background     = "#AABBCC"

为布局指定了固定的宽度和高度,以及背景颜色,上面的例子运行后效果如下图:

说明:对LinearLayout 中的参数android:layout_gravity 来说,其意义是指定本布局相对于父布局的重力方向,由于该布局的已经是根布局,其父布局是整个屏幕,那么该参数设置的是相对于屏幕的位置,可以换不同的参数top|bottom|left|right 等等参数来试验。

现在增加参数 android:gravity = "bottom|right" 完整 XML 如下,看看效果

 

 1 <? xml version = "1.0" encoding = "utf-8" ?>
 2 
 3 < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
 4 
 5                       android:layout_height = "200dp"
 6 
 7                       android:layout_width = "200dp"
 8 
 9                       android:background = "#AABBCC"
10 
11                       android:orientation="horizontal"
12 
13                       android:layout_gravity= "center"
14 
15                       android:gravity = "bottom|right " >
16 
17                       < TextView android:text = "ONE"
18 
19                                      android:background = "#aa0000"
20 
21                                      android:layout_height = "wrap_content"
22 
23                                      android:layout_width = "wrap_content"
24 
25                                      android:layout_margin = "1dp" />
26 
27                       < TextView android:text = "TWO"
28 
29                                      android:background = "#aa0000"
30 
31                                      android:layout_height = "wrap_content"
32 
33                                      android:layout_width = "wrap_content"
34 
35                                      android:layout_margin = "1dp" />
36 
37 </ LinearLayout >

 

 

通过改变android:gravity 参数的值可以看到实际效果。

参数 android:orientation= " horizontal " 决定了每个子元素各占一列,如果

参数 android:orientation= " vertical " 则每个子元素各占一行,也就是从上到下排列了。

对于 LinearLayout 布局的子元素,给每个子元素加上参数 android:layout_weight

看看效果

 

 1 <? xml version = "1.0" encoding = "utf-8" ?>
 2 
 3 < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
 4 
 5                       android:layout_height = "200dp"
 6 
 7                       android:layout_width = "200dp"
 8 
 9                       android:background = "#AABBCC"
10 
11                       android:layout_gravity = "center"
12 
13                       android:gravity = "bottom|right"
14 
15                       android:orientation = "horizontal" >
16 
17                       < TextView android:text = "ONE"
18 
19                                      android:background = "#aa0000"
20 
21                                      android:layout_height = "wrap_content"
22 
23                                      android:layout_width = "wrap_content"
24 
25                                      android:layout_margin = "1dp"
26 
27                                      android:layout_weight = "1" />
28 
29                       < TextView android:text = "TWO"
30 
31                                      android:background = "#aa0000"
32 
33                                      android:layout_height = "wrap_content"
34 
35                                      android:layout_width = "wrap_content"
36 
37                                      android:layout_margin = "1dp"
38 
39                                      android:layout_weight = "2" />
40 
41 </ LinearLayout >

 

Text 为ONE 的权重为1 ,但明显占的宽度比TWO 的小,百思不得其解,后来得知,如果把TextView 的参数android:layout_width = "wrap_content" 全部修改为 android:layout_width = "fill_parent", ok ,代码如下

 

 1 <? xml version = "1.0" encoding = "utf-8" ?>
 2 
 3 < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
 4 
 5                       android:layout_height = "200dp"
 6 
 7                       android:layout_width = "200dp"
 8 
 9                       android:background = "#AABBCC"
10 
11                       android:layout_gravity = "center"
12 
13                       android:gravity = "bottom|right"
14 
15                       android:orientation = "horizontal" >
16 
17                       < TextView android:text = "ONE"
18 
19                                      android:background = "#aa0000"
20 
21                                      android:layout_height = "wrap_content"
22 
23                                      android:layout_width = " fill_parent "
24 
25                                      android:layout_margin = "1dp"
26 
27                                      android:layout_weight = "1" />
28 
29                       < TextView android:text = "TWO"
30 
31                                      android:background = "#aa0000"
32 
33                                      android:layout_height = "wrap_content"
34 
35                                      android:layout_width = " fill_parent "
36 
37                                      android:layout_margin = "1dp"
38 
39                                      android:layout_weight = "2" />
40 
41 </ LinearLayout >

 

这篇关于android 布局中 layout_gravity、gravity、orientation、layout_weight的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1081522

相关文章

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

CSS弹性布局常用设置方式

《CSS弹性布局常用设置方式》文章总结了CSS布局与样式的常用属性和技巧,包括视口单位、弹性盒子布局、浮动元素、背景和边框样式、文本和阴影效果、溢出隐藏、定位以及背景渐变等,通过这些技巧,可以实现复杂... 一、单位元素vm 1vm 为视口的1%vh 视口高的1%vmin 参照长边vmax 参照长边re

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO

Android WebView的加载超时处理方案

《AndroidWebView的加载超时处理方案》在Android开发中,WebView是一个常用的组件,用于在应用中嵌入网页,然而,当网络状况不佳或页面加载过慢时,用户可能会遇到加载超时的问题,本... 目录引言一、WebView加载超时的原因二、加载超时处理方案1. 使用Handler和Timer进行超

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影