2010-05-30 24 views
7

Tôi đang cố gắng tạo bố cục máy tính bằng cách sử dụng TableLayout, nhưng hai hàng cuối cùng không phù hợp với phần còn lại của bố cục. Có điều gì sai với XML bố cục của tôi không?Cần trợ giúp với sự sắp xếp của Bảng điều khiển Android

Điều tôi đang cố gắng thực hiện sẽ dễ thực hiện hơn trong HTML (<td> với colspan hoặc rowspan), vì vậy tôi có nên thử chuyển đổi điều này thành WebView không?

Mã là như sau: (Screenshot)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <EditText 
     android:id="@+id/EditText01" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" /> 
    <TableLayout 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:stretchColumns="*"> 
     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"> 
      <Button 
       android:id="@+id/Button08" 
       android:textSize="16pt" 
       android:text="^" 
       android:layout_weight="1" 
       android:layout_height="fill_parent" 
       android:layout_width="wrap_content" /> 
      <Button 
       android:id="@+id/Button09" 
       android:text="÷" 
       android:textSize="16pt" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:layout_width="wrap_content" /> 
      <Button 
       android:id="@+id/Button10" 
       android:text="×" 
       android:textSize="16pt" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:layout_width="wrap_content" /> 
      <Button 
       android:id="@+id/Button11" 
       android:textSize="16pt" 
       android:text="-" 
       android:layout_weight="1" 
       android:layout_height="fill_parent" 
       android:layout_width="wrap_content" /> 
     </TableRow> 
     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="2"> 
      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:orientation="vertical" 
       android:layout_weight="1"> 
       <Button 
        android:id="@+id/Button01" 
        android:text="7" 
        android:textSize="16pt" 
        android:layout_weight="1" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" /> 
       <Button 
        android:layout_height="wrap_content" 
        android:textSize="16pt" 
        android:text="4" 
        android:id="@+id/Button05" 
        android:layout_weight="1" 
        android:layout_width="fill_parent" /> 
      </LinearLayout> 
      <LinearLayout 
       android:layout_height="fill_parent" 
       android:orientation="vertical" 
       android:layout_weight="1" 
       android:layout_width="fill_parent"> 
       <Button 
        android:id="@+id/Button02" 
        android:layout_height="wrap_content" 
        android:text="8" 
        android:textSize="16pt" 
        android:layout_weight="1" 
        android:layout_width="fill_parent" /> 
       <Button 
        android:layout_height="wrap_content" 
        android:textSize="16pt" 
        android:text="5" 
        android:id="@+id/Button06" 
        android:layout_weight="1" 
        android:layout_width="fill_parent" /> 
      </LinearLayout> 
      <LinearLayout 
       android:layout_height="fill_parent" 
       android:orientation="vertical" 
       android:layout_weight="1" 
       android:layout_width="fill_parent"> 
       <Button 
        android:id="@+id/Button03" 
        android:text="9" 
        android:textSize="16pt" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" /> 
       <Button 
        android:textSize="16pt" 
        android:text="6" 
        android:id="@+id/Button07" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" /> 
      </LinearLayout> 
      <Button 
       android:id="@+id/Button04" 
       android:text="+" 
       android:textSize="16pt" 
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent" 
       android:layout_weight="1" /> 
     </TableRow> 
     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="2"> 
      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:orientation="vertical" 
       android:layout_weight="2"> 
       <LinearLayout 
        android:layout_weight="1" 
        android:orientation="horizontal" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"> 
        <Button 
         android:id="@+id/Button02" 
         android:textSize="16pt" 
         android:layout_weight="1" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:text="1" /> 
        <Button 
         android:textSize="16pt" 
         android:id="@+id/Button06" 
         android:layout_weight="1" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:text="2" /> 
       </LinearLayout> 
       <Button 
        android:id="@+id/Button01" 
        android:layout_height="wrap_content" 
        android:textSize="16pt" 
        android:layout_weight="1" 
        android:layout_width="fill_parent" 
        android:text="0" /> 
      </LinearLayout> 
      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:orientation="vertical" 
       android:layout_weight="1"> 
       <Button 
        android:id="@+id/Button03" 
        android:textSize="16pt" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="3" /> 
       <Button 
        android:textSize="16pt" 
        android:id="@+id/Button07" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="." /> 
      </LinearLayout> 
      <Button 
       android:id="@+id/Button04" 
       android:textSize="16pt" 
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent" 
       android:layout_weight="1" 
       android:text="=" /> 
     </TableRow> 
    </TableLayout> 
</LinearLayout> 
+0

Rõ ràng nút '=' đang chiếm nhiều không gian hơn những gì bạn dự định. Hãy thử điều chỉnh tham số layout_weight của nút '='. – primpap

+0

"nhưng hai hàng cuối cùng không phù hợp với phần còn lại của bố cục" - Tôi không thể nhìn thấy màn hình của bạn từ đây. Bạn có thể đăng ảnh chụp màn hình những gì bạn đang thấy không? Nếu bạn có một mockup cho thấy những gì bạn đang cố gắng đạt được, thì càng nhiều càng tốt. – CommonsWare

+0

primalpop, tôi đã thay đổi trọng số thành 0 và bây giờ tôi có http://img683.imageshack.us/img683/2043/calculatorandroidlayout.png. Có cách nào mà tôi có thể có một ô bảng bao gồm nhiều hàng/cột mà không sử dụng LinearLayouts lồng nhau? CommonsWare, http://img185.imageshack.us/img185/2043/calculatorandroidlayout.png là phiên bản gốc và http://img683.imageshack.us/img683/2043/calculatorandroidlayout.png là phiên bản hiện tại. –

Trả lời

5

thức TableRow có ba loại tế bào (ví dụ, trẻ em ngay lập tức) của bạn. Phần còn lại có bốn. Do đó, chúng sẽ không sắp xếp trừ khi bạn đặt thuộc tính android:layout_span trong đó ở đâu đó để cho biết ô nào đang hấp thụ thứ tư.

+2

Tôi đặt "ô" chứa 0, 1 và 2 để kéo dài hai ô và đặt dấu bằng trở lại trọng số 1 và nó hoạt động như một nét duyên dáng! Cảm ơn! –

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