2011-01-01 35 views
11

Tôi có một TableLayout có nút ở dưới cùng. Vấn đề của tôi là nút kéo dài để lấp đầy toàn bộ chiều rộng của cột mặc dù tôi không muốn nó rộng.Chiều rộng nút trong Android TableLayout

alt text

XML trông như thế này:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:stretchColumns="*" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <TableRow android:layout_height="wrap_content" 
     android:layout_width="fill_parent"> 
    <CheckBox android:id="@+id/check1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Some text 1"/> 
    <CheckBox android:id="@+id/check2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Some text 1"/> 
    </TableRow> 
    <TableRow android:layout_height="wrap_content" 
     android:layout_width="fill_parent"> 
    <CheckBox android:id="@+id/check3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Even some more text 2"/> 
    <CheckBox android:id="@+id/check4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Even some more text 2"/> 
    </TableRow> 
    <TableRow android:layout_height="wrap_content" 
     android:layout_width="fill_parent"> 
    <CheckBox android:id="@+id/check5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="An even longer line of text 3"/> 
    <CheckBox android:id="@+id/check6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Another longer line of text 3"/> 
    </TableRow> 

    <EditText 
     android:id="@+id/myEditText1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textMultiLine" 
     android:singleLine="false" 
     android:text="Enter some text" 
    /> 

    <TableRow> 
    <Button android:id="@+id/SaveButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Save"> 
    </Button> 
    </TableRow>  

</TableLayout> 

Thậm chí nếu tôi đặt một chiều rộng rõ ràng để các nút nó bỏ qua nó. Ví dụ: nếu tôi có:

<Button android:id="@+id/SaveButton" 
     android:layout_width="100sp" 
     android:layout_height="100sp" 
     android:text="Save"> 
    </Button> 

... nút cao hơn nhưng chiều rộng vẫn trải dài để lấp đầy cột.

Tôi muốn nút này bằng một nửa chiều rộng hiện tại của nó, được căn giữa trong cột. Cảm ơn trước !!

Trả lời

20

@mbaird là đúng: android:stretchColumns="*" luôn kéo dài chiều rộng của cột. Tuy nhiên, bạn vẫn có thể nhận được hành vi mà bạn muốn bên trong cột bằng cách đặt một FrameLayout vào cột kéo dài chiều rộng của cột, và sau đó đặt nút của bạn bên trong đó. Sau đó, bạn có thể làm cho kích thước của nút tuy nhiên bạn thích và căn giữa kích thước trong FrameLayout:

<TableRow> 
    <FrameLayout 
     android:layout_height="wrap_content"> 
     <Button 
      android:id="@+id/SaveButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:paddingLeft="40dp" 
      android:paddingRight="40dp" 
      android:text="Save"> 
     </Button> 
    </FrameLayout> 
</TableRow> 
3

Tôi chắc chắn android:stretchColumns="*" sẽ luôn khiến nút được kéo dài như thế. Bạn đã cân nhắc đặt nút bên ngoài TableLayout chưa?

Từ tài liệu: Con của TableLayout không thể chỉ định thuộc tính layout_width. Chiều rộng luôn là MATCH_PARENT. Tuy nhiên, thuộc tính layout_height có thể được định nghĩa bởi một đứa trẻ; giá trị mặc định là WRAP_CONTENT

+0

Vấn đề với giải pháp này là khó đặt nút giữa cột, nếu nút nằm ngoài cột . –

Các vấn đề liên quan