2013-07-05 51 views
0

我想在表格行中排列三個按鈕。我似乎無法妥善地對其進行調整。如何在表格佈局中對齊按鈕?

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/TableLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" 
android:stretchColumns="*" > 

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

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 
</TableRow> 

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

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left" 
     android:layout_weight="0.33" 
     android:background="@drawable/bar" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="0dp" 
     android:layout_weight="0.33" 
     android:layout_height="wrap_content" 
     android:background="@drawable/pie" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="0dp" 
     android:layout_weight="0.33" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:background="@drawable/bar" /> 
</TableRow> 

我對佈局上面的代碼。我想在頂部有一個edittext,下面有3個按鈕。但是該按鈕超過了edittext,即使edittext的寬度被設置爲match_parent。誰能告訴我爲什麼會這樣?

Layout Screenshot

其實我是想以編程方式做到這一點。首先在xml中嘗試它,然後將其轉換爲編程方式。

在此先感謝。

回答

1

好吧,試試這個。基本上,如果您希望控件佔用多列,則必須使用layout_span來指示列的數量。

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

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:layout_weight="1" 
     android:layout_span="3"> 

     <requestFocus /> 
    </EditText> 
</TableRow> 

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

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left" 
     android:layout_weight="1" 
     android:background="@drawable/bar" 
    /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:background="@drawable/pie" 
     /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:background="@drawable/bar" 
     /> 
</TableRow> 
+0

完美!謝謝 :) – Newbie