Android中常用的五种布局方式:LinearLayout

我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前, 视图都是由一个一个的组件构成的。组件就是我们常见的Button、EditText等等。那么我们平时看到的Android手机中那些漂亮的界面是怎么显示出来的呢?这就要用到Android的布局管理器了。
网上有人比喻的很好:布局好比是建筑里的框架,组件按照布局的要求依次排列,就组成了用于看见的漂亮界面了。

LinearLayout
线性布局是按照水平或垂直的顺序将子元素(可以是控件或布局)依次按照顺序排列,每一个元素都位于前面一个元素之后。

常用的属性:
orientation(排列方式):布局中组件的排列方式,horizontal(水平,默认的),vertical(竖直),注意:如果是竖直的一定要写出来,水平的可以省略.

gravity:控制组件包含的子元素对其方式,可多个组合,如gravity=”left|bottom”.

layout_gravity:控制组件在父容器里的对其方式.

layout_width:布局宽度,一般不直接写数字的,wrap_content(自适应大小),match_parent或者fill_parent(充满父容器)

layout_height:布局的高度,参数如上

padding:控制子元素相对容器的距离.

layout_margin:控制组件在父容器里的相对距离

layout_weight(权重):分配完控件所需的空间,剩余屏幕按占有的比例分配.
if(wrap_content){ 权重值越大,占得屏幕比例越多,但小不过warp_contant}

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

    <Button
        android:layout_weight="99999"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="第一个button"/>
    <Button
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="第二个button"/>

</LinearLayout>

这里写图片描述
图 1
button1,button2获取的宽度为wrap_content,然后再分配剩余的空间,所以button1的比例越大,button2的比例小,button2的宽度最小还是wrap_content.

if(match_parent){
权重值越大,占得屏幕比例越小,但大大不过match_parent}

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

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="100000"
        android:text="第一个button" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="第二个button" />

</LinearLayout>

这里写图片描述
图 2
button1的宽度:match_parent-100000/100001*match_parent
button2的宽度:match_parent-1/100001*match_parent
所以button2的宽度接近于屏幕的宽度,button1的宽度接近于没有,被button2挤没了.

一般开发中是这样写的

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

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="第一个button" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="第二个button" />

</LinearLayout>

java代码设置权重
setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT, 1));

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值