2013-05-23 17 views
6

Tôi có tableLayout với hai cột và hai hàng, cả hai hàng và cột cuối cùng có match_parent cho chiều rộng nhưng bố cục không điền chiều rộng gốc, nó tự nó giống như nó có wrap_content .TableLayout với layout_width = matchparent không khớp với bố cục

Đây là mã:

<TableLayout android:layout_width="match_parent"> 
    <TableRow android:layout_width="match_parent"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:text="static" /> 
     <EditText 
      android:layout_width="match_parent" 
      android:text="text" /> 
    </TableRow> 

    <TableRow android:layout_width="match_parent"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:text="static2" /> 
     <EditText 
      android:layout_width="match_parent" 
      android:text="text2" /> 
    </TableRow> 
</TableLayout> 

gì tôi cần phải làm cho mỗi dòng có độ rộng của phụ huynh?

Ps: nơi tôi làm việc không cho phép tôi đăng mã của mình, vì vậy tôi viết mã càng gần càng tốt mã của mình. Tôi không biết nếu nó là đúng, tôi không thể kiểm tra.

Trả lời

20

Hãy thử mã này tôi nghĩ rằng wil này giúp bạn

<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    > 

    <TableRow android:layout_width="match_parent" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_weight="1" 
      android:text="static" /> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_weight="1" 
      android:text="text" /> 
    </TableRow> 

    <TableRow android:layout_width="match_parent" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_weight="1" 
      android:text="static2" /> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_weight="1" 
      android:text="text2" /> 
    </TableRow> 
</TableLayout> 
+3

'android: layout_weight =" 1 "' trong 'TableRow' của tôi đã thực hiện thủ thuật cho tôi. Cảm ơn! – Alias

1

Cũng xem xét nếu có các hàng khác có nội dung rộng hơn so với các hàng khác có chứa một TextView, vì thuộc tính trọng lượng sẽ không hoạt động tốt. Hãy xem xét ví dụ sau:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/diseno" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

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

      <TextView 
       android:id="@+id/nombre" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/nombre" /> 

       <EditText 
        android:id="@+id/campo1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="text" /> 

    </TableRow> 

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

     <TextView 
      android:id="@+id/apellidos" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/apellidos" 
      android:layout_weight="1"/> 

     <EditText 
      android:id="@+id/campo2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:inputType="text" 
      android:layout_weight="1" /> 

    </TableRow> 

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

     <CheckBox 
      android:id="@+id/tick" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checked="false" 
      android:text="@string/¿Quieres suscribirte a la página?"/> 

    </TableRow> 

Vì vậy, đây là kết quả:

  • Các tế bào hàng thứ ba là thiết lập mỗi chiều rộng di động còn lại.
  • Hàng đầu tiên EditText bắt đầu khi ô bên trái kết thúc (gần ba phần tư chiều rộng màn hình). Dòng thứ hai được đặt trọng số 1 mỗi ô, vì vậy cả hai ô trong hàng phải đo nửa màn hình, nhưng vì ô đầu tiên rộng, trọng lượng được tính theo tỷ lệ, vì vậy ngay cả khi bạn đặt trọng số là 50 đối với ô bên phải, bạn sẽ không bao giờ nhận được một nửa màn hình vì ô đầu tiên không thể nhỏ hơn nội dung trên ô thứ ba.
Các vấn đề liên quan