[Android基础]布局属性

参考链接:

http://blog.csdn.net/shakespeare001/article/details/7843460

http://blog.csdn.net/xxdbupt/article/details/20450915

http://www.xuebuyuan.com/2083533.html


1.android:gravity

这个是针对控件里的元素来说的,用来控制元素在该控件里的显示位置。

android:gravity="bottom|right"


2.android:layout_gravity(当父控件为RelativeLayout,这个属性无效)

这个是针对控件本身而言,用来控制该控件在包含该控件的父控件中的位置。

android:layout_gravity="center_horizontal"


特殊情况

当我们采用LinearLayout布局时,有以下特殊情况需要我们注意:

(1)当 android:orientation="vertical"  时, android:layout_gravity只有水平方向的设置才起作用,垂直方向的设置不起作用(因为垂直方向已经交给系统管理了)。

即:left,right,center_horizontal 是生效的。

(2)当 android:orientation="horizontal" 时, android:layout_gravity只有垂直方向的设置才起作用,水平方向的设置不起作用(因为水平方向已经交给系统管理了)。

即:top,bottom,center_vertical 是生效的。


3.android:padding

Padding 为内边框,指该控件内部内容,如文本/图片与该控件的距离

android:paddingLeft="10dp"
android:paddingRight="100dp"



4.android:layout_margin

Margin 为外边框,指该控件与其他控件(不一定是父控件)的距离

android:layout_marginLeft="10dp"
android:layout_marginRight="100dp"


如果左右上下都是相同的设置则可以直接设置

android:layout_margin="10dip" 
android:padding="5dip"


第三四个属性跟第一二个属性在作用上都很相似,不同点是第三四个属性赋值为某个值,而第一二个属性赋值为上下左右等

上面的属性要么是控件内部元素跟控件的位置关系,要么是控件跟父控件的位置关系。如果是控件与控件之间的关系,就可以使用下面的属性。


5.

android:layout_above="@id/xxx"  --将控件的下边缘和给定ID控件的上边缘对齐
android:layout_below="@id/xxx"  --将控件的上边缘和给定ID控件的下边缘对齐

android:layout_toLeftOf="@id/xxx"  --将控件的右边缘和给定ID控件的左边缘对齐
android:layout_toRightOf="@id/xxx"  --将控件的左边缘和给定ID控件的右边缘对齐

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  
  
    <Button  
        android:id="@+id/1"  
        android:layout_width="100dp"  
        android:layout_height="100dp"  
        android:layout_centerInParent="true"  
        android:text="hello1" />  
      
    <Button  
        android:id="@+id/2"  
        android:layout_width="100dp"  
        android:layout_height="100dp"   
        android:layout_toLeftOf="@id/1"  
        android:text="hello2" />  
      
    <Button  
        android:id="@+id/3"  
        android:layout_width="100dp"  
        android:layout_height="100dp"   
        android:layout_toRightOf="@id/1"  
        android:text="hello3" />  
      
    <Button  
        android:id="@+id/4"  
        android:layout_width="100dp"  
        android:layout_height="100dp"  
        android:layout_above="@id/1"  
        android:text="hello4" />  
      
    <Button  
        android:id="@+id/5"  
        android:layout_width="100dp"  
        android:layout_height="100dp"  
        android:layout_below="@id/1"  
        android:text="hello5" />  
      
</RelativeLayout>



6.

android:layout_alignLeft="@id/xxx"  --将控件的左边缘和给定ID控件的左边缘对齐
android:layout_alignTop="@id/xxx"  --将控件的上边缘和给定ID控件的上边缘对齐
android:layout_alignRight="@id/xxx"  --将控件的右边缘和给定ID控件的右边缘对齐
android:layout_alignBottom="@id/xxx"  --将控件的底边缘和给定ID控件的底边缘对齐

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  
  
    <Button  
        android:id="@+id/1"  
        android:layout_width="100dp"  
        android:layout_height="100dp"  
        android:layout_centerInParent="true"  
        android:text="hello1" />  
      
    <Button  
        android:id="@+id/2"  
        android:layout_width="200dp"  
        android:layout_height="50dp"   
        android:layout_alignLeft="@id/1"  
        android:text="hello2" />  
      
    <Button  
        android:id="@+id/3"  
        android:layout_width="200dp"  
        android:layout_height="50dp"   
        android:layout_alignRight="@id/1"
        android:layout_below="@id/2"
        android:text="hello3" />  
      
    <Button  
        android:id="@+id/4"  
        android:layout_width="50dp"  
        android:layout_height="150dp"  
        android:layout_alignTop="@id/1"  
        android:text="hello4" />  
      
    <Button  
        android:id="@+id/5"  
        android:layout_width="50dp"  
        android:layout_height="150dp"  
        android:layout_alignBottom="@id/1"
        android:layout_toRightOf="@id/4"
        android:text="hello5" />  
      
</RelativeLayout>



7.

android:layout_alignParentLeft="true"  --将控件的左边缘和父控件的左边缘对齐
android:layout_alignParentTop="true"  --将控件的上边缘和父控件的上边缘对齐
android:layout_alignParentRight="true"  --将控件的右边缘和父控件的右边缘对齐
android:layout_alignParentBottom="true" --将控件的底边缘和父控件的底边缘对齐


android:layout_centerInParent="true"  --将控件置于父控件的中心位置
android:layout_centerHorizontal="true"  --将控件置于水平方向的中心位置
android:layout_centerVertical="true"  --将控件置于垂直方向的中心位置


8.

注意:android:layout_margin为外边距,不一定是该控件跟父控件的距离

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  
  
    <Button  
        android:id="@+id/1"  
        android:layout_width="100dp"  
        android:layout_height="100dp"  
        android:layout_centerInParent="true"  
        android:text="hello1" />

    <Button
        android:id="@+id/2"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_below="@+id/1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp"
        android:text="hello2" />
   
</RelativeLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值