Android layout_weight案例分析总结
16lz
2021-01-23
关于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,表示该控件不会被拉伸以适应布局;其他值则表示,控件在布局空白区域中的占地比例。
结合实际例子来看看:
<?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"> > > > :id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="9"> > >
上面的布局整体由一个竖向的LinearLayout填满画布,然后再由一个横向的LinearLayout和一个listView组成它的两部分,横向LinearLayout包含两个按钮。我们主要看的是listview的摆放。显然,按钮的比重需要小,列表的比重需要大。所以,我们通过制定layout_weight一小一大,就可以了,分别为1和9,当然个人喜好问题了。
至于按钮和listview的宽和高,我们需要根据实际情况来布局,一般来说,在下面的布局中,我们需要将宽度和画布的宽度设置为一样大,不然就不好看了。而高度则是以字体的大小来定的,所以设置为wrap_content即可。
更多相关文章
- Android--取得布局中指定控件的宽高
- Android oncreate中获取控件宽高
- android计算器布局(TableLayout)
- 控件 - ImageView
- 四大布局
- android开发4:Android布局管理器1(线性布局,相对布局RelativeLayou
- RelativeLayout(相对布局)
- Android基础————UI布局之高级控件详解
- Android常用控件—TextView及其子类Button、EditText等