在做android界面设计时如果使用到TableLayout布局即表格布局,要想让界面中的控件填满整个屏幕除了要设置TableLayout的属性
android:layout_width="fill_parent"
android:layout_height="fill_parent"
还需要设置TableRow的属性为
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
注意这里的android:layout_weight="1"一定不能忘了,否则子控件的android:layout_height="fill_parent"是不起作用的。
最后就是界面控件的属性了,这里以Button为例来做演示
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Button" />
和上面的一样这里的android:layout_weight="1"也不能少,这个保证按钮能在横向上铺满屏幕。
完整的布局文件代码如下:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Button" />
</TableRow>
</TableLayout>
效果如下: