Android 线性布局属性及详解

线性布局的特点是:各个子元素彼此连接,中间不留空白

线性布局是程序中最常见的一种布局方式,线性布局可以分为水平线性布局和垂直线性布局两种,通过android:orientation属性可以设置线性布局的方向。下面是一个简单的线性布局的例子:

线性布局中,有 个及其重要的参数,直接决定元素的布局和位置,这几个参数是

android:layout_gravity ( 是本元素相对于父元素的对齐方式 )

android:gravity="bottom|right"(是本元素所有子元素的对齐方式,设置在父元素上,多个值用|隔开)

android:layout_gravity (子元素在父元素的对齐方式,设置在子元素上)

当 android:orientation="vertical"  时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。
当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。

android:padding="10dp" (是本元素所有子元素的与父元素边缘的距离,设置在父元素上)

android:layout_marginLeft="10dp"(子元素与父元素边缘的距离,设置在子元素上)

android:orientation (线性布局以列或行来显示内部子元素)

android:layout_weight ="1"(线性布局内子元素对未占用空间【水平或垂直】分配权重值,其值越小,权重越大

前提是子元素 设置了 android:layout_width "fill_parent" 属性(水平方向) android:layout_height "fill_parent" 属性(垂直方向)如果某个子元素的 android:layout_width "wrap_content"  android:layout_height =" wrap_content” 则 android:layout_weight 的设置值 对该方向上空间的分配刚好相反。

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version = "1.0" encoding = "utf-8"?>  
  2. <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"  
  3.   android:layout_height = "fill_parent"  
  4.   android:layout_width = "fill_parent"  
  5.   android:background = "#AABBCC"  
  6.   android:orientation= "horizontal"  
  7.   android:gravity="bottom|right"  
  8.    >  
  9.      <TextView android:text = "哈哈"             
  10.                android:background = "#aa0000"  
  11.                android:layout_height = "wrap_content"  
  12.                android:layout_width = "fill_parent"  
  13.                android:layout_marginLeft="10dp"  
  14.                android:layout_weight="1"  
  15.                android:layout_gravity="center"  
  16.                />  
  17.     <TextView android:text = "呵呵"  
  18.               android:background = "#aa0000"  
  19.               android:layout_height = "wrap_content"  
  20.               android:layout_width = "fill_parent"  
  21.               android:layout_marginRight="5dp"  
  22.               android:layout_marginLeft="5dp"  
  23.               android:layout_weight="2"  
  24.               />  
  25. </LinearLayout >  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值