Android布局中涉及的一些属性

news/2024/7/6 1:09:32

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>
复制代码

效果如下图

image

盒模型

为了更加准确地控制TextView里面内容的位置,我们可以使用一系列的padding属性来控制。在使用padding属性之前,先科普一下padding和Marigin之间的区别,然后我们在通过实际的效果看看他们之间的差异。

下图所示是一个类似盒子的模型,我们将通过下面的模型来讲解Padding和Marigin之间的区别。从图中可以看出,在Container(父控件)里面有一个子控件,假设是一个TextView控件。其中Margin是子控件与父控件之间的间隔大小。Border是子控件的边框,它是子控件和父控件的边界。Padding是指子控件中的内容(Content Area)与子控件Border的间隔大小。

image

margin属性

Android中有一系列的margin属性,下面让我们看看其中的android:layout_marginRight属性,为了有一个对比的效果,我们先将marginRight设为0dip,再将其设为50dip,如以下两图所示

android:layout_marginRight="0dip"

android:layout_marginRight="50dip"

clip_image002[6]

image

从上图中,我们可以看出,左图TextView控件跟他的父控件的是没有右间隔的,而右图明显的有一块间隔(见右图黄色圈圈部分)。

marginRight相同的还有以下属性,它们的原理都相同,就不一一细讲了。

属性名相关方法描述
android:layout_marginBottomsetMargins(int,int,int,int)Specifies extra space on the bottom side of this view.
android:layout_marginEndsetMarginEnd(int)Specifies extra space on the end side of this view.
android:layout_marginLeftsetMargins(int,int,int,int)Specifies extra space on the left side of this view.
android:layout_marginRightsetMargins(int,int,int,int)Specifies extra space on the right side of this view.
android:layout_marginStartsetMarginStart(int)Specifies extra space on the start side of this view.
android:layout_marginTopsetMargins(int,int,int,int)Specifies extra space on the top side of this view.

 

padding属性

下面让我们来看看android:layout_paddingRight属性。我们将在以下布局中,通过改变android:layout_paddingRight属性,来看看变化。

image

为了有一个对比的效果,我们先将paddingRight设为0dip,再将其设为50dip,如以下两图所示

android:layout_paddingRight="0dip"android:layout_paddingRight="50dip"
imageimage

从上图中,我们可以看出,左图TextView控件中的内容跟TextView的边框(border)是没有右间隔的,而右图明显的有一块间隔(见右图黄色圈圈部分)。

paddingRight相同的还有以下属性,它们的原理都相同,就不一一细讲了。

属性名相关方法描述
android:paddingsetPaddingRelative(int,int,int,int)Sets the padding, in pixels, of all four edges.
android:paddingBottomsetPaddingRelative(int,int,int,int) Sets the padding, in pixels, of the bottom edge; see padding.
android:paddingEndsetPaddingRelative(int,int,int,int) Sets the padding, in pixels, of the end edge; see padding.
android:paddingLeftsetPadding(int,int,int,int) Sets the padding, in pixels, of the left edge; see padding.
android:paddingRightsetPadding(int,int,int,int) Sets the padding, in pixels, of the right edge; see padding.
android:paddingStartsetPaddingRelative(int,int,int,int) Sets the padding, in pixels, of the start edge; see padding.
android:paddingTopsetPaddingRelative(int,int,int,int) Sets the padding, in pixels, of the top edge; see padding.

 

示例代码


作者:kissazi2 
出处:http://www.cnblogs.com/kissazi2/ 
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载:http://www.cnblogs.com/kissazi2/p/3309109.html


http://www.niftyadmin.cn/n/4446343.html

相关文章

TensorFlow 历史版本 API(转)

https://blog.csdn.net/u014061630/article/details/81193504

一个快速完整的教程,以保存和恢复Tensorflow模型。(转)

https://blog.csdn.net/tan_handsome/article/details/79303269

js图片放大缩小

2019独角兽企业重金招聘Python工程师标准>>> 下载地址&#xff1a;http://pan.baidu.com/s/1bB74Ns 转载于:https://my.oschina.net/u/555061/blog/679742

TypeError: a bytes-like object is required, not 'str'(转)

https://blog.csdn.net/qq_41185868/article/details/83833262

xlslib库使用简记

xlslib库使用简记 1 前言 最近需要使用C结合xlslib库来生成Excel文件&#xff0c;但发现这个库的文档还真难找&#xff0c;找来找去发现唯一的线索是有一个test/目录里面的几个例子而已。 想到以后要不断的和这个库打交道&#xff0c;除非愿意用Python去重写&#xff0c;但吃力…

数据库内核月报 - 2015 / 10-MySQL · 特性分析 · 跟踪Metadata lock

背景 MySQL 从5.5.3版本&#xff0c;对Metadata lock进行了调整&#xff0c;主要是MDL锁持有的周期从语句变成了事务&#xff0c; 其原因主要是解决两个问题&#xff1a; 问题1: 破坏事务隔离级别 在repeatable read的隔离级别下&#xff0c;多次的select语句执行过程中&#x…

深度强化学习——Dueling-DDQN(转)

https://blog.csdn.net/u013236946/article/details/73161586