android layout_weight难点解析

在Android开发汇中,如果能够很好的理解layout_weight的好处,便能够很快的布局出你想要的布局,极大的提高开发效率。下面我根据我所学的对layout_weight理解做个记录。

首先看这样一个布局:

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:background="#f00"
        android:gravity="center"
        android:text="1sdfsdfsffsfsf" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="2"
        android:background="#d68787"
        android:gravity="center"
        android:text="2" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="3"
        android:background="#6ae444"
        android:gravity="center"
        android:text="3" />


</LinearLayout>
显示的效果是这样:图1

图1------->图2

估计预览图和大家的猜想稍有不一致。为什么呢?这是因为子view的内容跟随父view的基线,所以解决此问题的方式是:在LinearLayout中添加属性android:baselineAligned="false"即可,如图2。

如果这样3个textView是下面这样:

 <pre name="code" class="html"><TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:background="#f00"
        android:gravity="center"
        android:text="1sdfsdfsffsfsf" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="2"
        android:background="#d68787"
        android:gravity="center"
        android:text="2" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="3"
        android:background="#6ae444"
        android:gravity="center"
        android:text="3" />

 效果图如图3 

图3--------图4

如果代码是这样:

 <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:background="#f00"
        android:gravity="center"
        android:text="11111111111" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_weight="2"
        android:background="#d68787"
        android:gravity="center"
        android:text="2" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_weight="2"
        android:background="#6ae444"
        android:gravity="center"
        android:text="3" />
那么效果图又是图4了。至于为什么是这样呢,原因在于:


所以得出结论:

LinearLayout中的layout_weight属性首先按照控件声明的尺寸进行分配,然后再将剩下的尺寸按weight分配。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值