对于android:layout_weight的用法主要看两点:
1: android:layout_weight的值越小说明优先级越高,首先被渲染。
2:
在layout_width設置為fill_parent的時候,layout_weight所代表的是你的控件要首先尽可能的大,但這個大是有限度的,即fill_parent.
在layout_width設置為wrap_content的時候,layout_weight所代表的是你的控件要首先尽可能的小,但這個小是有限度的,即wrap_content.
1. layout_width="fill_parent", button1的weight=1,button2的weight=2;如下图:
理解:先看比重,值越小先被渲染,所以button1先出来。
再看layout_width,如果为"fill_parent"则说明这个button1要尽可能的大,具体大多少还要看其他控件的weight的值。
2.layout_width="fill_parent", button1的weight=1,button2的weight=2000;如下图:
这时的button2也是存在的,只是非常小了。
3.layout_width="wrap_content", button1的weight=1,button2的weight=2;如下图:
理解:先看比重,值越小先被渲染,所以button1先出来。
再看layout_width,如果为"wrap_content"则说明这个button1要尽可能的小,具体小多少还要看其他控件的weight的值。
4.layout_width="wrap_content", button1的weight=1,button2的weight=2000;如下图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_gravity=""
android:text="Button2"
/>
</LinearLayout>