Android layout_weight案例分析总结

关于layout_weight,我们一般就理解为布局权重的意思,Android官方的说明如下:
Formats: float
Indicates how much of the extra space in the LinearLayout is allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.

可赋值类型为float,
layout_weight的含义是:在线性布局中,这些控件各自占据的空白区域的比重。
为0,表示该控件不会被拉伸以适应布局;其他值则表示,控件在布局空白区域中的占地比例。

结合实际例子来看看:
Android layout_weight案例分析总结_第1张图片

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

    :layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        

上面的布局整体由一个竖向的LinearLayout填满画布,然后再由一个横向的LinearLayout和一个listView组成它的两部分,横向LinearLayout包含两个按钮。我们主要看的是listview的摆放。显然,按钮的比重需要小,列表的比重需要大。所以,我们通过制定layout_weight一小一大,就可以了,分别为1和9,当然个人喜好问题了。
至于按钮和listview的宽和高,我们需要根据实际情况来布局,一般来说,在下面的布局中,我们需要将宽度和画布的宽度设置为一样大,不然就不好看了。而高度则是以字体的大小来定的,所以设置为wrap_content即可。
Android layout_weight案例分析总结_第2张图片
Android layout_weight案例分析总结_第3张图片

你可能感兴趣的:(Android百万案例之路)