从零开始学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个参数

<? xml version = "1.0" encoding = "utf-8" ?>

< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"

                           android:layout_height = "200dp"

                           android:layout_width = "200dp"

                           android:background = "#AABBCC"

                           android:orientation= "horizontal"

                           android:layout_gravity= "center" >

                           < TextView android:text = "ONE"

                                               android:background = "#aa0000"

                                               android:layout_height = "wrap_content"

                                               android:layout_width = "wrap_content"

                                               android:layout_margin = "1dp" />

                            < TextView android:text = "TWO"

                                               android:background = "#aa0000"

                                               android:layout_height = "wrap_content"

                                               android:layout_width = "wrap_content"

                                               android:layout_margin = "1dp" />

</ 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 如下,看看效果

 

<? xml version = "1.0" encoding = "utf-8" ?>

< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"

                      android:layout_height = "200dp"

                      android:layout_width = "200dp"

                      android:background = "#AABBCC"

                      android:orientation="horizontal"

                      android:layout_gravity= "center"

                      android:gravity = "bottom|right " >

                      < TextView android:text = "ONE"

                                     android:background = "#aa0000"

                                     android:layout_height = "wrap_content"

                                     android:layout_width = "wrap_content"

                                     android:layout_margin = "1dp" />

                      < TextView android:text = "TWO"

                                     android:background = "#aa0000"

                                     android:layout_height = "wrap_content"

                                     android:layout_width = "wrap_content"

                                     android:layout_margin = "1dp" />

</ LinearLayout >

 

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

 

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

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

 

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

看看效果

<? xml version = "1.0" encoding = "utf-8" ?>

< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"

                      android:layout_height = "200dp"

                      android:layout_width = "200dp"

                      android:background = "#AABBCC"

                      android:layout_gravity = "center"

                      android:gravity = "bottom|right"

                      android:orientation = "horizontal" >

                      < TextView android:text = "ONE"

                                     android:background = "#aa0000"

                                     android:layout_height = "wrap_content"

                                     android:layout_width = "wrap_content"

                                     android:layout_margin = "1dp"

                                     android:layout_weight = "1" />

                      < TextView android:text = "TWO"

                                     android:background = "#aa0000"

                                     android:layout_height = "wrap_content"

                                     android:layout_width = "wrap_content"

                                     android:layout_margin = "1dp"

                                     android:layout_weight = "2" />

</ LinearLayout >

 

 

 

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

 

<? xml version = "1.0" encoding = "utf-8" ?>

< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"

                      android:layout_height = "200dp"

                      android:layout_width = "200dp"

                      android:background = "#AABBCC"

                      android:layout_gravity = "center"

                      android:gravity = "bottom|right"

                      android:orientation = "horizontal" >

                      < TextView android:text = "ONE"

                                     android:background = "#aa0000"

                                     android:layout_height = "wrap_content"

                                     android:layout_width = " fill_parent "

                                     android:layout_margin = "1dp"

                                     android:layout_weight = "1" />

                      < TextView android:text = "TWO"

                                     android:background = "#aa0000"

                                     android:layout_height = "wrap_content"

                                     android:layout_width = " fill_parent "

                                     android:layout_margin = "1dp"

                                     android:layout_weight = "2" />

</ LinearLayout >

 

转载于:https://www.cnblogs.com/dekevin/p/4290143.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/459617.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Thread详解

具体可参考&#xff1a;Java并发编程&#xff1a;Thread类的使用&#xff0c;这里对线程状态的转换及主要函数做一下补充。 一. 线程状态转换图 注意&#xff1a; 调用obj.wait()的线程需要先获取obj的monitor&#xff0c;wait()会释放obj的monitor并进入等待态。所以wait()/no…

mac怎么用终端编写c语言视频,【新手提问】有知道用mac终端编c语言的网络编程的人吗?...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼#include#include#include#include#include#include#define ECHOMAX 255int main(int argc,char *argv[]){ int sock;struct sockaddr_in echoServAddr;struct sockaddr_in echoClntAddr;unsigned short echoServPort;unsigned int…

Net框架下-ORM框架LLBLGen的简介(转载)

Net框架下-ORM框架LLBLGen的简介 http://www.cnblogs.com/huashanlin/archive/2015/02/12/4288522.html 官方网址&#xff1a;http://www.llblgen.com/转载于:https://www.cnblogs.com/wangjunwei/p/4290896.html

加强团队凝聚力建设方面采取的方法

加强团队凝聚力建设方面采取的方法主要有&#xff1a;1. 项目经理定期和团队成员进行单独沟通&#xff0c;了解成员对工作和个人职业发展的一些真实想法&#xff0c;使团队发展和个人发展两者相互促进&#xff0c;让团队成员感受到在做研发过程中个人技能的提高和个人成就感的增…

c语言实现线性表的算法,数据结构算法代码实现——线性表的定义(一)

线性表的定义线性表&#xff1a;是最常用且最简单的一种数据结构&#xff0c;它是一种线性数据结构&#xff0c;是由类型相同的n个(n≥0)数据元素组成的有序序列。线性表的特点&#xff1a;有且只有一个被称作“第一个”的数据元素&#xff0c;有且只有一个被称为“最后一个”的…

uva 10716 Evil Straw Warts Live

没有看明白这标题配图和题目有什么关系&#xff0c;好像这是一张专辑的名字。先要考虑有多少个字母总数是奇数&#xff0c;超过1个就直接输出impossible。每一次考虑两端点情况&#xff0c;如果字母不同&#xff0c;再进行搜索&#xff0c;换那个字母交换次数比较少&#xff0c…

嵌入式 boa服务器移植

随着Internet技术的兴起&#xff0c;在嵌入式设备的管理与交互中&#xff0c;基于Web方式的应用成为目前的主流&#xff0c;这种程序结构也就是大家非常熟悉的B/S结构&#xff0c;即在嵌入式设备上运行一个支持脚本或CGI功能的Web服务器&#xff0c;能够生成动态页面&#xff0…

linux 欢迎语,一日一技 | 如何让你的终端欢迎语好看又有趣

原标题&#xff1a;一日一技 | 如何让你的终端欢迎语好看又有趣Matrix 精选Matrix 是少数派的写作社区&#xff0c;我们主张分享真实的产品体验&#xff0c;有实用价值的经验与思考。我们会不定期挑选 Matrix 最优质的文章&#xff0c;展示来自用户的最真实的体验和观点。文章代…

springmvc 1

springmvc的model是实体类&#xff0c;可以理解为把数据库里的一张表变成了一个对象 /*** */ package com.test.model;/*** ClassName: User.java* Description: TODO(用一句话描述该文件做什么) * * author JerryZhou* Date 2014-7-15 上午10:24:04 *…

android java adb命令大全,Android adb命令备份恢复手机信息

假设你已经在Windows下安装了Android SDK&#xff0c;并且更新到最新版步骤&#xff1a;1.通过USB连接你的设备&#xff0c;打开命令行2.一般地&#xff0c;输入”adb devices“检测设备是否连接正常有个命令“ adb backup”(简化写法)可以使你备份整个系统。这个命令的参数如下…

【HDOJ】【3037】Saving Beans

排列组合 啊……这题是要求c(n-1,0)c(n,1)c(n1,2)......c(nm-1,m) 这个玩意……其实就等于c(nm,m) 好吧然后就是模P……Lucas大法好 我SB地去预处理<P的所有fac和inv了……果断TLE 事实上Lucas时对于<P的部分直接暴力算就好了 1 //HDOJ 30372 #include<cstdio>3 #…

php远程下载文件

<?php /* 本源码来源于网络 http://user.qzone.qq.com/292672703 */ header("content-Type: text/html; charsetutf-8"); //定义编码 set_time_limit (0);//不限时 24 * 60 * 60 //语言包数组 $lang_cn array (0 > 文件地址,1 > 输入密码,2 > 下载耗…

linux 下安装部署mq,RocketMQ在linux下安装部署

本博客以当前RocketMQ最新版介绍&#xff1a;v4.4.0环境要求64位JDK 1.8;Maven 3.2.x; // 源码编译时需要用到二进制文件安装> unzip rocketmq-all-4.4.0-bin-release.zip && mv rocketmq-all-4.4.0-bin-release rocketmq启动server> cd /root/rocketmq> nohu…

一个典型的参数型跨站脚本漏洞

拿百度主页曾经的一个XSS做个演示&#xff0c;这个漏洞是由于百度主页tn和bar参数过滤不严导致的参数型XSS&#xff1a;http://www.baidu.com/index.php?tn"/**/stylexss:expression(alert(‘xss‘)); http://www.baidu.com/index.php?bar"/**/stylexss:expressio…

java 常用工具类的使用一

1. Java工具概述 很多人初学程序时&#xff0c;总是在想&#xff0c;那么多的算法该怎么写呀&#xff1f;那么多的数据结构都不熟悉&#xff0c;该怎么实现呀&#xff1f;总是担心英语不好程序学不精通&#xff0c;数学不好写程序无法达到巅峰。学的程序越多&#xff0c;不懂的…

android http最新框架,Android框架学习笔记02AndroidAsycHttp框架

上一篇中我们介绍了OkHttp3.0框架的基本使用方法&#xff0c;这一篇我们学习一下Android的另外一个网络请求框架——AsyncHttpClient框架。Asynchttpclient框架是一个开源的异步网络请求框架&#xff0c;所有的网络都在Android的非UI线程中&#xff0c;通过回调方法处理请求结果…

C# 读取app.config配置文件 节点键值,提示 配置系统未能初始化 错误的解决方案...

新建C#项目&#xff0c;在app.config中添加了appSettings项&#xff0c;运行时出现"配置系统未能初始化"的错误&#xff0c;MSDN里写到&#xff0c;如果配置文件中包含 configSections 元素&#xff0c;则 configSections 元素必须是 configuration 元素的第一个子元…

miui12 android系统耗电,miui12耗电严重怎么办,miui12续航优化方法

很多小米用户反馈升级到miui12稳定版后耗电大大增加&#xff01;再大的电池也经不住miui12的耗电&#xff01;那么miui12耗电严重怎么办&#xff1f;miui12续航优化的方法呢&#xff1f;一起和XDA小编看看吧&#xff01;近日&#xff0c;有网友对此提供了优化miui12耗电的方法&…

Linux下 执行程序

看到有人问Linux下的./表示什么意思&#xff0c;我就趁机在这里写一下个人愚见&#xff1a; ./的意思是执行当前目录下的某可执行文件. . /相当于 source 根目录下的一个脚本. 转载于:https://www.cnblogs.com/zuiyirenjian/p/4299322.html

r6400 usb android,网件R6400路由器USB接口速率测试与总结

网件R6400路由器USB接口速率测试测试中使用的是浦科特M6V 256GB固态硬盘搭配USB3.0硬盘盒进行测试&#xff0c;此外由于无线网络速率有一定的波动&#xff0c;对最终测试成绩测试成绩有较大的影响&#xff0c;因此我们使用网速相对稳定的千兆有线网络连接路由器进行测试。首先我…