Android 之 线性布局

[size=medium][color=green][b]线性布局:
线性布局是最简单,Android开发者使用的最多的布局类型之一,开发者用它来组织
用户界面上的控件。线性布局的作用就像他的名字一样:它将控件组织在一个垂直或
水平的形式;

在线性布局中,左右的子元素都按照垂直或水平的顺序在界面上进行排列:
* 如果垂直排列,则:表现的形式为一列多行,且每行只包含一个界面元素;
* 如果水平排列:则:表现的形式为一行多列,且每列只包含一个界面元素;

案例如下:
效果实现:垂直排列,位于屏幕的右下角!





使用说明:
属性 对应方法 描述
android:orientation setOrientation 设置线性布局的朝向,值:horizontal /vertical
android:gravity setGravity(int) 设置线性布局的内部元素的布局方式

给子控件赋权:(layout_weight)
与其他线性布局属性不同,其它属性应用在线性布局视图本身,而layout_weight是应用在在它的
子控件上的。权值本身应该是一个数字(如:0.5 0.25),如果把所有子空间的权值加起来等于1,
子控件的权值控制它在父线性布局中有多“重要”或者留给其多少"空间"

案例如下:
效果实现:垂直排列,按比重0.2 0.5 0.3 进行效果的实现;[/b][/color][/size]

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<!--
效果实现:垂直排列,并在屏幕的右下角

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
-->



<!-- 权值的使用:效果实现,垂直排列 比重:0.2 0.5 0.3 -->

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:background="#F5F5DC"
android:gravity="center"
android:text="权值:0.2\n 水平垂直居中" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#00CED1"
android:gravity="center"
android:text="权值:0.5\n 水平垂直居中" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:background="#FFD700"
android:gravity="center"
android:text="权值:0.3\n 水平垂直居中" />

</LinearLayout>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值