4 Android 线性布局 、相对布局

1、线性布局(LinearLayout)
线性布局是Android中较为常用的布局方式,它使用标签表示。线性布局有两种方式指定控件位置,一种是水平方向,一种是竖直方向。
LinearLayout详解
①常用属性

orientation:布局中组件的排列方式,有horizontal(水平)vertical(垂直,默认)
gravity:控制组件所包含的子元素的对齐方式,可多个组合,如(left/buttom)
layout_gravity: 控制该组件在父容器里的对齐方式
layout_width:布局的宽度,通常不直接写数字的,用wrap_content(组件实际大小)或者match_parent值满父容器
layout_height: 布局的高度,参数同上
id:为该组件设置一个资源id,在java文件中可以通过findViewByld(id)找到该组件
background:为该组件设置一个背景图片,或者直接用颜色覆盖

②Weight(权重)
该属性是用来等比例地划分区域
1)最简单的用法:要等比例划分,分谁,谁为0.weight按比例即可
2)Weight使用详解

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        android:text="按钮一"
        android:layout_weight="0"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        android:text="按钮二"
        android:layout_weight="1"/>
</LinearLayout>

③divider分割线
该属性用于为LinearLayout设置分割线图片,通过showDividers来设置分割线的所在位置,有四个可选值none,middle(中间),begining(开始),end;还可以通过

divider:为LinearLayout设置分割线图片
dividerpadding:设置分割线padding

2、相对布局(Relativelayout)
在Eclipse中开发Android程序时,默认采用的就是相对布局。
相对布局通常有两种形式,一种是相对于容器而言的,一种是相对于控件而言的。
Relativelayout详解
①基本属性

gravity :设置容器内组件的对齐方式
ignoreGravity:设置了该属性为true的属性的组件,将不受gravity属性的影响

②根据父容器定位

layout_alighParentLeft:左对齐
layout_alighParentRight:右对齐
layout_alighParentTop:顶部对齐
layout_alighParentBottom:底部对齐
android:layout_centerHorizontal :水平居中
android:layout_centerVertical:垂直居中
android:layout_centerInParent:中间居中

③根据兄弟组件定位

layout_toLeftOf :参考组件的左边
layout toRightOf: 参考组件的右边
layout_above:参考组件的上方
layout_below :参考组件的下方
layout_alignTop:对齐参考组件的上边界
layout alignBottom :对齐参考组件的下边界
layout_alignLeft:对齐参考组件的左边界
layout_alignRight:对齐参考组件的右边界

④margin(偏移):设置组件与父容器的边距,又叫偏移

layout_margin:设置组件上下左右的偏移量
layout_marginLeft:设版组件离左边的偏移量
layout_marginRight:设置组件离右边的偏移量
layout_marginTop:设置组件离上面的偏移量
layout_marginButtom:设置组件离下面的偏移量

⑤padding(填充):设置组件内部元素间的边距(比如Textview里的字体位置)

android:padding:往内部元素的上下左右填充一定边距
paddingLeft:往内部元素的左边填充一定边距
paddingRight:往内部元素的右边填充一定边距
paddingTop:往内部元素的上方填充一定边距
paddingBottom:往内部元素的下方填充一定边距

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值