Android UI杂谈---layout_weight

来源:互联网 发布:小学生机器人编程 编辑:程序博客网 时间:2024/05/24 05:09

对Android 中 LinearLayout中属性layout_weight讨论:
(一)
(1)当android:orientation=”horizontal”android:layout_width=”0dp”时:

<?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="match_parent"        android:layout_weight="1"        android:background="#C82D25"        android:text="test activity1" />    <TextView        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="2"        android:background="#EBF0F3"        android:text="test activity2" />    <TextView        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="3"        android:background="#F2CACA"        android:text="test activity3" /></LinearLayout>

表现:
成正比
(2)当
android:orientation=”horizontal”android:layout_width=”match_parent”时:

<?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="match_parent"        android:layout_height="match_parent"        android:layout_weight="1"        android:background="#C82D25"        android:text="test activity1" />    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="2"        android:background="#EBF0F3"        android:text="test activity2" />    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="3"        android:background="#F2CACA"        android:text="test activity3" /></LinearLayout>

表现:这里写图片描述

(3)当
android:orientation=”horizontal”且 android:layout_width=”wrap_content”时:

<?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="wrap_content"        android:layout_height="match_parent"        android:layout_weight="1"        android:background="#C82D25"        android:text="test activity1" />    <TextView        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_weight="2"        android:background="#EBF0F3"        android:text="test activity2" />    <TextView        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_weight="3"        android:background="#F2CACA"        android:text="test activity3" /></LinearLayout>

表现:
这里写图片描述
总结:
表现最佳的是第一种,第二种把第三个TextView挤掉了,第三种没有按比例排列,完全失去了layout_weight属性的作用。
从源码角度分析:
google LinearLayout源码:

            if (widthMode == MeasureSpec.EXACTLY && lp.width == 0 && lp.weight > 0) {                // Optimization: don't bother measuring children who are going to use                // leftover space. These views will get measured again down below if                // there is any leftover space.                if (isExactly) {                    mTotalLength += lp.leftMargin + lp.rightMargin;                } else {                    final int totalLength = mTotalLength;                    mTotalLength = Math.max(totalLength, totalLength +                            lp.leftMargin + lp.rightMargin);                }                // Baseline alignment requires to measure widgets to obtain the                // baseline offset (in particular for TextViews). The following                // defeats the optimization mentioned above. Allow the child to                // use as much space as it wants because we can shrink things                // later (and re-measure).                if (baselineAligned) {                    final int freeWidthSpec = MeasureSpec.makeSafeMeasureSpec(                            MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.UNSPECIFIED);                    final int freeHeightSpec = MeasureSpec.makeSafeMeasureSpec(                            MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.UNSPECIFIED);                    child.measure(freeWidthSpec, freeHeightSpec);                } else {                    skippedMeasure = true;                }            }

例子:
如果有这样的需求:
这里写图片描述
右边的箭头不想因为左边的字内容太多被挤掉可以这样写:

<LinearLayout            android:id="@+id/select_deal_card_list_click"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginLeft="13dp"               android:background="@drawable/selector_card_list_bg_end"            android:clickable="true"            android:orientation="horizontal"            android:gravity="center"            android:padding="7dp" >           <TextView                android:id="@+id/select_deal_card_list_content"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:text="2111111112222222/卡"                android:singleLine="true"                android:ellipsize="end"                android:layout_weight="1"                android:textColor="@color/common_gray_txt_bg" />            <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:src="@drawable/select_ico" />        </LinearLayout>
0 0
原创粉丝点击