Android布局中涉及的一些属性
Android:gravity属性
线性布局常见的就是利用LinearLayout进行布局,其中有个比较重要的属性就是android:gravity,在官方文档中是这么描述这个属性的:指定一个元素怎么放置它的内容,包括在X和Y轴,在它自己的边框中。
下面我们将在一个简单的TextView中应用android:gravity属性。假设我们想要TextView内的内容在右侧显示,那么我们就可以编写对应的XML布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding_medium"
android:background="#ffffff"
android:gravity="right"
android:text="@string/hello_world"
android:textColor="#ff0000"
android:textSize="@dimen/font_size" />
</LinearLayout>
效果如下图
盒模型
为了更加准确地控制TextView里面内容的位置,我们可以使用一系列的padding属性来控制。在使用padding属性之前,先科普一下padding和Marigin之间的区别,然后我们在通过实际的效果看看他们之间的差异。
下图所示是一个类似盒子的模型,我们将通过下面的模型来讲解Padding和Marigin之间的区别。从图中可以看出,在Container(父控件)里面有一个子控件,假设是一个TextView控件。其中Margin是子控件与父控件之间的间隔大小。Border是子控件的边框,它是子控件和父控件的边界。Padding是指子控件中的内容(Content Area)与子控件Border的间隔大小。
margin属性
Android中有一系列的margin属性,下面让我们看看其中的android:layout_marginRight属性,为了有一个对比的效果,我们先将marginRight设为0dip,再将其设为50dip,如以下两图所示
android:layout_marginRight="0dip" |
android:layout_marginRight="50dip" |
|
|
从上图中,我们可以看出,左图TextView控件跟他的父控件的是没有右间隔的,而右图明显的有一块间隔(见右图黄色圈圈部分)。
与marginRight相同的还有以下属性,它们的原理都相同,就不一一细讲了。
属性名 | 相关方法 | 描述 |
android:layout_marginBottom | setMargins(int,int,int,int) | Specifies extra space on the bottom side of this view. |
android:layout_marginEnd | setMarginEnd(int) | Specifies extra space on the end side of this view. |
android:layout_marginLeft | setMargins(int,int,int,int) | Specifies extra space on the left side of this view. |
android:layout_marginRight | setMargins(int,int,int,int) | Specifies extra space on the right side of this view. |
android:layout_marginStart | setMarginStart(int) | Specifies extra space on the start side of this view. |
android:layout_marginTop | setMargins(int,int,int,int) | Specifies extra space on the top side of this view. |
padding属性
下面让我们来看看android:layout_paddingRight属性。我们将在以下布局中,通过改变android:layout_paddingRight属性,来看看变化。
为了有一个对比的效果,我们先将paddingRight设为0dip,再将其设为50dip,如以下两图所示
android:layout_paddingRight="0dip" | android:layout_paddingRight="50dip" |
![]() |
![]() |
从上图中,我们可以看出,左图TextView控件中的内容跟TextView的边框(border)是没有右间隔的,而右图明显的有一块间隔(见右图黄色圈圈部分)。
与paddingRight相同的还有以下属性,它们的原理都相同,就不一一细讲了。
属性名 | 相关方法 | 描述 |
android:padding | setPaddingRelative(int,int,int,int) | Sets the padding, in pixels, of all four edges. |
android:paddingBottom | setPaddingRelative(int,int,int,int) | Sets the padding, in pixels, of the bottom edge; see padding . |
android:paddingEnd | setPaddingRelative(int,int,int,int) | Sets the padding, in pixels, of the end edge; see padding . |
android:paddingLeft | setPadding(int,int,int,int) | Sets the padding, in pixels, of the left edge; see padding . |
android:paddingRight | setPadding(int,int,int,int) | Sets the padding, in pixels, of the right edge; see padding . |
android:paddingStart | setPaddingRelative(int,int,int,int) | Sets the padding, in pixels, of the start edge; see padding . |
android:paddingTop | setPaddingRelative(int,int,int,int) | Sets the padding, in pixels, of the top edge; see padding . |
Android布局中涉及的一些属性的更多相关文章
- android布局中使用include及需注意点
在android布局中,使用include,将另一个xml文件引入,可作为布局的一部分,但在使用include时,需注意以下问题: 一.使用include引入 如现有标题栏布局block_header ...
- Android布局中的空格以及占一个汉字宽度的空格的实现
在Android布局中进行使用到空格,以便实现文字的对齐.那么在Android中如何表示一个空格呢? 空格: 窄空格: 一个汉字宽度的空格: [用两个空格( )占一个汉字的宽度时,两个空格比 ...
- Android布局中的空格以及占一个汉字宽度的空格,实现不同汉字字数对齐
前言 在Android布局中进行使用到空格,以便实现文字的对齐.那么在Android中如何表示一个空格呢? 空格: (普通的英文半角空格但不换行) 窄空格: (中文全角空格 (一个中文宽度)) ...
- Android布局中的layout_weight和weightSum属性的详解及使用
由于Android设备的尺寸大小不一,种类繁多,当我们在开发应用的时候就要考虑屏幕的适配型了,尽可能让我们的应用适用于主流机型的尺寸,这样我们的应用不会因为尺寸不同而不美观,解决屏幕适配问题的方法有很 ...
- Android 布局中 如何使控件居中
首先要分两种不同情况,在两种不同的布局方式下:LinearLayout 和RelativeLayout 1. LinearLayout a). android:layout_gravity=" ...
- 【转】在Android布局中使用include和merge标签
内容转自:http://fengweipeng1208.blog.163.com/blog/static/21277318020138229754135/ 在我们开发android布局时,经常会有很多 ...
- Android布局中 android:layout_gravity="bottom"为何不起作用?
在android布局时我们有时会需要将位于LinearLayout布局中的控件放在布局底部,或者是同时想将几个控件底部对齐,此时我们自然会想到使用 android:layout_gravity=&qu ...
- android 布局中 layout_gravity、gravity、orientation、layout_weight
线性布局中,有 4 个及其重要的参数,直接决定元素的布局和位置,这四个参数是 android:layout_gravity ( 是本元素相对于父元素的重力方向 ) android:gravity (是 ...
- android布局中显示隐藏动画
android 在布局中提供属性,能简单的加入动画效果,例如以下: <LinearLayout ... animateLayoutChanges="true" ... /&g ...
随机推荐
- Windows 7 下如何设置机器级别的DCOM权限
Windows 7 下如何设置机器级别的DCOM权限 To grant Remote Activation permissions to the SMS Admins group From the S ...
- jsoncpp操作 json
jsoncpp操作 json 博客分类: c/c++ object-c 之 iphone #include <iostream> //#include "json/json. ...
- php 乱码解决
1)首先确定你的终端编码,如果你不知道如何确定,分别执行这两段代码,看看哪个能输出中文. PHP code ? 1 echo pack("H12","E4B8ADE6 ...
- WebForm 简单控件、复合控件
简单控件: Label:被编译成span 样式表里设置lable的高度: display:inline-block; Text --文本 ForeColor --字体颜色 Visible -- ...
- oracle根据分隔符将字符串分割成数组函数
--创建表类型 create or replace type mytype as table of number;--如果定义成varchar--CREATE OR REPLACE type myty ...
- Python UTF8 读取文件
严禁伸手党:
- [2015hdu多校联赛补题]hdu5378 Leader in Tree Land
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:给你一棵n个结点的有根树.因为是有根树,那么每个结点可以指定以它为根的子树(后面讨论的子树 ...
- Selenium2+python自动化23-富文本(自动发帖)
前言 富文本编辑框是做web自动化最常见的场景,有很多小伙伴遇到了不知道无从下手,本篇以博客园的编辑器为例,解决如何定位富文本,输入文本内容 一.加载配置 1.打开博客园写随笔,首先需要登录,这里为了 ...
- 学习springMVC框架配置遇到的问题-数据写入不进数据库时的处理办法
配置完了,运行,数据写入不到数据库中,就应该想UserAction 中的handleRequest()方法有没有进去,然后就设置断点.如果发现程序没有进去,就再想办法进去.
- Python.Scrapy.14-scrapy-source-code-analysis-part-4
Scrapy 源代码分析系列-4 scrapy.commands 子包 子包scrapy.commands定义了在命令scrapy中使用的子命令(subcommand): bench, check, ...