gravity、layout_gravity及orientation
gravity、layout_gravity及orientation
最近在弄一个简单的界面:横向,添加一张准备好的背景图,在界面右边居中放置一个按钮。实现过程中发现对布局的主要属性没有想象中地那么熟悉,又重新找资料树梳理了一遍,这里总结一下。
1、gravity、layout_gravity及orientation基本概念
gravity,中文意思为重心(理科生不会陌生吧),表示组件(View)横向和纵向的停靠位置。如果不进行任何设置,默认是靠左。
android:gravity,对组件本身来说的,作用是设置表示组件包含的内容显示在表示组件的什么位置,默认值为左侧。最常见的例子就是组件上的文本,如android:gravity=”center”,那么文本显示位置为垂直和水平方向均居中。
android:layout_gravity,相对于包含该组件(元素)的父组件来说的,设置该组件在父组件的显示位置。若android:layout_gravity="center_vertical",那么该组件在父组件中的位置为垂直方向居中,但水平方向可能是靠左的。orientation,线性布局时以列或行来显示内部子元素,注意在其他布局一般用不到该属性,如AbsoluteLayout等。该属性默认是水平排列(horizontal),即以行的形式来布置包含的子组件,在实际应用程序的开发中用得较多是垂直排布,即不一定要进行如下设定:android:orientation=”vertical”。
2、gravity与layout_gravity属性介绍
top,Put the object at the top of its container, not changing its size。将对象放在其容器的顶部,不改变其大小。
bottom,Put the object at the bottom of its container, not changing its size。将对象放在其容器的底部,不改变其大小。
left,Put the object at the left edge of its container, not changing its size。将对象放在其容器的左侧,不改变其大小。
right ,Put the object at the right edge of its container, not changing its size。将对象放在其容器的右侧,不改变其大小。
center_vertical,Place object in the vertical center of its container, not changing its size。将对象纵向居中,不改变其大小。垂直对齐方式:垂直方向上居中对齐。
fill_vertical,Grow the vertical size of the object if needed so it completely fills its container。必要的时候增加对象的纵向大小,以完全充满其容器。垂直方向填充。
center_horizontal,Place object in the horizontal center of its container, not changing its size。将对象横向居中,不改变其大小。水平对齐方式:水平方向上居中对齐
fill_horizontal, Grow the horizontal size of the object if needed so it completely fills its container。必要的时候增加对象的横向大小,以完全充满其容器。
center,Place the object in the center of its container in both the vertical and horizontal axis, not changing its size。将对象横纵居中,不改变其大小。
fill,Grow the horizontal and vertical size of the object if needed so it completely fills its container。This is the default。必要的时候增加对象的横纵向大小,以完全充满其容器。水平方向填充。
clip_vertical,Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds。 The clip is based on the vertical gravity: a top gravity clips the bottom edge, a bottom gravity clips the top edge, and neither clips both edges。附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容。 剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部。垂直方向裁剪。
clip_horizontal,Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds。 The clip is based on the horizontal gravity: a left gravity clips the right edge, a right gravity clips the left edge, and neither clips both edges。附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容。 剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧。水平方向裁剪。
3、xml代码实现
界面的横向设置与背景图的添加很容易实现,针对按钮靠右居中,刚开始没有想到利用orientation这个属性,想当然地以为layout_gravity能够搞定,所以过程有点波折。
整体设计思路为:最外层放置一个LinearLayout,设置子组件的布局为垂直方向居中并靠右,代码如下:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|right"
android:background="@drawable/login_bg"
里层再放置一个LinearLayout,用来放置具体的按钮。通过上一步的设置,该LinearLayout显示在了界面的右边,并且是垂直方向居中(其实一般应用中是让该LinearLayout的layout_height属性为match_parent,故父组件中gravity属性的center_vertical值可以不添加)。
到这里,完成了大半,接下来就是添加按钮。注意,目标是让按钮在界面上靠右并居中。刚开始将内层LinearLayout的gravity属性设置为垂直居中,纵向铺满屏幕,为了美观,让其距界面右边界40个像素无关点。代码如下:
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_marginRight="@dimen/margin_Right"
但是问题来了,添加三个按钮,以为能够达到预想的效果:水平与垂直方向上均居中地排布在内层的LinearLayout中,并且第二个在第一个下面,第三个在第二个下面。然而,现实是这样的:
后面看了一些例子,恍然大悟,子组件的纵向或横向上的排布,需要借助orientation属性。马上加入代码:
android:orientation="vertical"
满意的结果出现了:
三个按钮的实现代码如下:
<Button android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/login_by_weixin"
android:textColor="@string/color_weixin"
android:textSize="@dimen/margin_Right" /> <Button android:id="@+id/button2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/logout_by_weixin"
android:textColor="@string/color_weixin"
android:textSize="@dimen/margin_Right" /> <Button android:id="@+id/button3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/close"
android:textColor="@string/color_weixin"
android:textSize="@dimen/margin_Right" />
代码中对按钮的大小、具体位置、文本(颜色、大小)进行了设置。
可以发现,若仅将LinearLayout的gravity属性设置为垂直居中,那么当子组件个数大于1时,会全部排列在居中位置,放不下只会在水平方向上错位显示,并不会从上到下依次排布,此时就需要借助orientation属性了。
gravity、layout_gravity及orientation的更多相关文章
- gravity layout_gravity
gravity:控制当前视图的内容/子view layout_gravity:控制视图本身
- Android中的layout_gravity和gravity的区别
在Android的布局中,除了padding和margin容易弄混之外,还有layout_gravity和gravity.按照字面意思来说,layout_gravity就是相对于layout来设置的. ...
- 带你实现开发者头条APP(三) 首页实现
title: 带你实现开发者头条APP(三) 首页实现 tags: 轮播广告,ViewPager切换,圆形图片 grammar_cjkRuby: true --- 一.前言 今天实现开发者头条APP的 ...
- FragmentTabHost的基本用法
开通博客以来已经约莫1个月了.几次想提笔写写东西,但总是由于各种各样的原因并没有开始.现在,年假刚结束,项目也还没有开始,但最终促使我写这篇博客的是,看了一篇博友写的新年计划,说是要在新的一年中写50 ...
- Android—关于自定义对话框的工具类
开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...
- 给ListView设置emptyView
给ListView设置emptyView 版权声明:本文为博主原创文章,未经博主允许不得转载. 使用ListView和GridView时,当列表为空时,默认是不显示任何内容的,这样对用户非常不友好,这 ...
- Android开发案例 - 自定义虚拟键盘
所有包含IM功能的App(如微信, 微博, QQ, 支付宝等)都提供了Emoji表情之类的虚拟键盘, 如下图: 本文只着重介绍如何实现输入法键盘和自定义虚拟键盘的流畅切换, 而不介绍如何实现虚 ...
- Android音视频之MediaPlayer音视频播放
前言: 昨天总结了视频录制,今天来学习一下视频的播放,Android的视频播放主要采用MediaPlayer类. MediaPlayer介绍 MediaPlayer类可用于控制音频/视频文件或流的播放 ...
- 仿优酷Android客户端图片左右滑动(自动滑动)
最终效果: 页面布局main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayou ...
随机推荐
- C#邮件发送问题(二)
C#邮件发送问题(一) 三.C#下创建基于TcpClient发送邮件组件 在上一节在Dos命令行下测试SMTP服务器连接时,已经使用了SMTP的部分命令,但是当时无法对信息进行编码和解码,也就无法继续 ...
- iNeedle系统之国舜项目
一.简介 本周公司接了一个小项目,是给北京国舜科技股份有限公司做一个HTTP相关的小功能产品.大概实现功能是将交换机的源数据通过解析,分析出HTTP包配对的request和response头,并把每对 ...
- 基于vitamio的网络电视直播源码
这个项目是基于vitamio的网络电视直播源码,也是一个使用了vitamio的基于安卓的网络直播项目源码,可能现在网上已经有很多类似这样的视频播放应用了,不过这个还是相对来说比较完整的,希望这个案例能 ...
- ListView的基础应用
在写完基础的布局之后,下一课我们会学习一下如何使用Android中一个非常重要,但是对于新手略有困难的ListView,甚至很久以前都有人说过,会不会写ListView是Android能否入门的第一步 ...
- Eclipse调试Bug的七种常用技巧
1. 条件断点 断点大家都比较熟悉,在Eclipse Java 编辑区的行头双击就会得到一个断点,代码会运行到此处时停止. 条件断点,顾名思义就是一个有一定条件的断点,只有满足了用户设置的条件,代码才 ...
- CF687A. NP-Hard Problem[二分图判定]
A. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 410C.Team[构造]
C. Team time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- [No00003A]操作系统Operating Systems 内核级线程Kernel Threads内核级线程实现Create KernelThreads
开始核心级线程 内核级线程对多核的支持怎么样? 和用户级相比,核心级线程有什么不同? ThreadCreate 是系统调用,内核管理TCB ,内核负责切换线程 如何让切换成型? − − 内核栈,TCB ...
- jQuery $.each用法
以下内容非原创,来自百度文库http://wenku.baidu.com/view/4796b6145f0e7cd18425368e.html 好文要顶 关注我 收藏该文 mabel_on_lin ...
- Oracle 中 decode 函数用法(转)
含义解释: decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下: IF 条件=值1 THEN RETURN(翻译值1) ELSIF 条件=值2 THE ...