2012-10-15 38 views
13

Tôi muốn vẽ một bảng trong đó cột cuối cùng phải nằm ở bên phải nhất của bảng.Cột căn chỉnh phải trong bố cục bảng android

Đây là cách dòng của bảng trông giống như:

Admin (2)New 
Network (2)New 

Và đây là cách nó nên là:

Admin (2)   New 
Network (2)  New 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<!-- row.xml --> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="45dp" 
    android:gravity="center" android:background="@color/list_bg"> 

    <TableLayout android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <TableRow> 



      <ImageView android:id="@+id/t1" android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

      <TextView android:id="@+id/t2" android:typeface="normal" 
       android:singleLine="true" android:textSize="14sp" android:textStyle="normal" 
       android:layout_width="wrap_content" android:textColor="#000000" 
       android:layout_height="wrap_content" /> 
      <TextView android:id="@+id/t10" android:typeface="normal" 
       android:singleLine="true" android:text=" " android:textSize="14sp" 
       android:textStyle="normal" android:layout_width="wrap_content" 
       android:textColor="#000000" android:layout_height="wrap_content" /> 
      <TextView android:id="@+id/t4" android:typeface="normal" 
       android:visibility="gone" android:singleLine="true" android:text="(" 
       android:textSize="14sp" android:textStyle="normal" 
       android:layout_width="wrap_content" android:textColor="#000000" 
       android:layout_height="wrap_content" /> 
      <TextView android:id="@+id/t5" android:typeface="normal" 
       android:visibility="gone" android:singleLine="true" 
       android:textSize="14sp" android:textStyle="normal" 
       android:layout_width="wrap_content" android:textColor="#000000" 
       android:layout_height="wrap_content" /> 
      <TextView android:id="@+id/t6" android:typeface="normal" 
       android:visibility="gone" android:singleLine="true" android:text=")" 
       android:textSize="14sp" android:textStyle="normal" 
       android:layout_width="wrap_content" android:textColor="#000000" 
       android:layout_height="wrap_content" /> 
      <ImageView android:id="@+id/t3" android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
      <TextView android:id="@+id/t7" android:typeface="normal" 
       android:visibility="visible" android:singleLine="true" android:text="New" 
       android:textSize="14sp" 
       android:textStyle="normal" android:layout_width="wrap_content" 
       android:textColor="#000000" android:layout_height="wrap_content" /> 

     </TableRow> 

    </TableLayout> 

</RelativeLayout> 

Trong t7 xml này nên ngay bên phải của bảng, làm thế nào để làm điều này?

+0

Bạn có thể nhóm LinearLayout và các chế độ xem khác để làm cho TableRow của bạn có nhiều cột hơn. Vì vậy, thay vì có một hàng bảng duy nhất với 8 cột, bạn sẽ có một hàng duy nhất với hai cột (như ví dụ của bạn). – Cookster

Trả lời

21

Ở đây bạn phải làm

  1. thiết lập chiều rộng bảng hàng để fill_parent

  2. và thiết lập android:layout_gravity="right" đến TextView mà bạn muốn sắp xếp đúng nó vào dòng của bảng

    ---- hoặc ----

  1. thêm trọng số vào chế độ xem văn bản bên trong hàng của bảng để chúng có thể căn chỉnh theo cách bạn muốn.
+0

Đây là những gì tôi đã làm Nhưng nó không làm việc??? –

+0

Làm cách nào để làm điều thứ hai? –

+0

bạn có nhận được yêu cầu của mình với phương pháp đầu tiên không? –

19
  1. android: stretchColumns = "x" trong đó x là cột bạn muốn điền
  2. android: layout_width = "fill_parent" để đảm bảo tất cả mọi thứ lấp đầy màn hình
  3. android: layout_gravity = "right" cho thành phần chế độ xem trong cột "x"

ví dụ (mã giả):

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="1"> 

    <TableRow> 
     <TextView android:text="Left Column" /> 
     <TextView android:layout_gravity="right|center_vertical" android:text="Right Adjusted Column" /> 
    </TableRow> 

</TableLayout> 
+0

Tầm quan trọng của # 1 là bố cục bảng điển hình bị chi phối bởi một cột chứa nhiều dữ liệu nhất. Nếu cột đó là hai chiều cao của hàng bảng, tất cả có thể có kích thước khác nhau. – Cookster

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