2012-03-15 31 views
12

Có và phương pháp nào để làm cho bảng như html trong android nơi tôi có thể thiết lập độ rộng cột hàng như chúng ta làm trong htmlLàm thế nào để thiết lập chiều rộng bảng và số lượng cột trong TableLayout trong Android

<table > 
<tr><td style="width:'50%;"></td> 
    <td style="width:'50%;"></td> 
</tr> 
</table> 

<TableLayout 
      android:id="@+id/tableLayout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:stretchColumns="3" > 
      <TableRow 
        android:id="@+id/tableRow1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" > 
      </TableRow> 
      <TableRow 
        android:id="@+id/tableRow1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" > 
      </TableRow> 
</TableLayout> 

Sửa 1:

Đối với một cái gì đó giống như Html có các công trình rộng tỷ lệ phần trăm đối với Phụ huynh

nhưng trọng lượng được giới thiệu trong Android hoạt động liên quan đến kích thước màn hình có sẵn cho số gốc.

Trả lời

22

Bạn có thể sử dụng LinearLayout và thuộc tính weight để đạt được điều này.

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:weightSum="1.0"> 

Các phần tử con trong hàng của bạn có thể được tính theo trọng lượng như một phần của tổng (phần trăm). Hãy chắc chắn để thiết lập các layout_width để "0dp"

<Button 
android:layout_height="wrap_content" 
android:layout_width="0dp" 
android:layout_weight="0.5" /> 

<Button 
android:layout_height="wrap_content" 
android:layout_width="0dp" 
android:layout_weight="0.5" /> 

Kiểm tra câu hỏi này trước Linear Layout and weight in Android

16

Các TableLayout có tính chất tương tự như LinearLayout. Tôi đã làm một số tweek vào nó quá. Để nói tốt hơn, hãy xem mã mẫu của tôi bên dưới. Hy vọng nó hữu ích.

<TableLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:stretchColumns="3" > 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="1"> 
      <TextView android:text="@string/test1" android:layout_weight="1"></TextView> 
      <TextView android:text="@string/test2" android:layout_weight="1"></TextView> 
      <TextView android:text="@string/test3" android:layout_weight="1"></TextView> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="1"> 
      <TextView android:text="@string/test1" android:layout_weight="1"></TextView> 
      <TextView android:text="@string/test2" android:layout_weight="1"></TextView> 
      <TextView android:text="@string/test3" android:layout_weight="1"></TextView> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="1"> 
      <TextView android:text="@string/test1" android:layout_weight="1"></TextView> 
      <TextView android:text="@string/test2" android:layout_weight="1"></TextView> 
      <TextView android:text="@string/test3" android:layout_weight="1"></TextView> 
     </TableRow> 

    </TableLayout> 
+0

Số cột là số không dựa trên độ dàiCác cột phải là 2 – user648026

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