如何布局2个按钮

时间:2011-08-07 12:08:36

标签: java android uitableview android-layout

我希望屏幕中央出现2个按钮,但屏幕底部会显示..

这是我到目前为止所拥有的。

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

    <TableLayout android:layout_width="match_parent"
     android:id="@+id/tableLayout2" 
    android:layout_gravity="center"
     android:layout_height="fill_parent"
     android:stretchColumns="@string/app_name">

        <TableRow android:id="@+id/tableRow1"
         android:layout_width="wrap_content" 
        android:layout_height="wrap_content">

            <Button android:text="History"
             android:layout_height="wrap_content" 
             android:layout_gravity="center_horizontal" 
             android:layout_width="fill_parent" 
             android:id="@+id/button1"
            ></Button>
            <Button android:text="RecordSpending" 
            android:layout_height="wrap_content"
             android:layout_width="fill_parent" 
             android:id="@+id/button2"
             android:gravity="center_vertical"></Button>
        </TableRow>

    </TableLayout>
</LinearLayout>

我希望整个布局显示在屏幕的底部..(无论用户有什么电话分辨率)..

我希望两列的大小相等,底部水平位于中心。

1 个答案:

答案 0 :(得分:3)

要在屏幕底部放置两个按钮,您可以执行此操作,伪代码添加相关属性。

<!-- This is the parent layout -->    
<RelativeLayout
 android:layout_height="fill_parent"
 android:layout_width="fill_parent" >

     <!-- Layout for your buttons at bottom -->
     <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_alignParentBottom="true">
        <!-- weight = 1 would mean both buttons are of equal width -->
        <Button
        android:layout_height="wrap_parent"
        android:layout_width="0dp"
        android:layout_weight="1.0" />        
        <Button
        android:layout_height="wrap_parent"
        android:layout_width="0dp"
        android:layout_weight="1.0" />

     </LinearLayout>
</RelativeLayout>