Android布局中涉及的一些属性

来源:互联网 发布:ug线切割编程 编辑:程序博客网 时间:2024/04/29 04:49

Android:gravity属性

    线性布局常见的就是利用LinearLayout进行布局,其中有个比较重要的属性就是android:gravity,在官方文档中是这么描述这个属性的:指定一个元素怎么放置它的内容,包括在X和Y轴,在它自己的边框中。

下面我们将在一个简单的TextView中应用android:gravity属性。假设我们想要TextView内的内容在右侧显示,那么我们就可以编写对应的XML布局

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#000000"
  6.     android:orientation="vertical" >
  7.  
  8.     <TextView
  9.         android:layout_width="fill_parent"
  10.         android:layout_height="wrap_content"
  11.         android:layout_margin="@dimen/padding_medium"
  12.         android:background="#ffffff"
  13.         android:gravity="right"
  14.         android:text="@string/hello_world"
  15.         android:textColor="#ff0000"
  16.         android:textSize="@dimen/font_size" />
  17. </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"clip_image002[6]

android:layout_marginRight="50dip"

 

从上图中,我们可以看出,左图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.

由于本人初写博客,写的不好的地方还请大家能批评指正,希望能和大家相互学习、相互交流、共同成长。

0 0