2011-01-18 33 views
30

Câu hỏi này kể cho tôi từ các bước đầu tiên của tôi với Android. Tôi không thể tạo cả hai cột trong TableLayout 2 cột chính xác 50% mỗi cột.TableLayout 2 cột với 50% chính xác cho mỗi cột

Dưới đây là một ví dụ:

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

    <TableLayout 
     android:id="@+id/tablelayout" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:paddingRight="2dip" > 

     <TableRow> 
      <TextView 
       style="@style/TextViewStandard" 
       android_layout_span="2" 
       android:layout_weight="1" 
       android:text="Bla" /> 
     </TableRow> 

     <TableRow> 
      <TextView 
       style="@style/TextViewStandard" 
       android:layout_weight="1" 
       android:text="Name:" /> 

      <EditText 
       style="@style/EditTextStandard" 
       android:id="@+id/et_name" 
       android:layout_weight="1" /> 
     </TableRow> 

     <TableRow> 
      <TextView 
       style="@style/TextViewStandard" 
       android:layout_weight="1" 
       android:text="URL:" /> 

      <EditText 
       style="@style/EditTextStandard" 
       android:id="@+id/et_url" 
       android:layout_weight="1" /> 
     </TableRow> 

     <TableRow> 
      <Button 
       style="@style/ButtonStandard" 
       android:layout_column="0" 
       android:layout_marginTop="6dip" 
       android:onClick="onClickOk" 
       android:text="@android:string/ok" /> 
     </TableRow> 
    </TableLayout> 
</ScrollView> 

Và đây là định nghĩa kiểu tương ứng:

<resources> 
    <style name="ButtonStandard" parent="@android:style/Widget.Button"> 
     <item name="android:layout_height">wrap_content</item> 
     <item name="android:layout_width">fill_parent</item> 
    </style> 

    <style name="EditTextStandard" parent="@android:style/Widget.EditText"> 
     <item name="android:layout_height">wrap_content</item> 
     <item name="android:layout_marginLeft">2dip</item> 
     <item name="android:layout_marginRight">2dip</item> 
     <item name="android:layout_marginTop">2dip</item> 
     <item name="android:layout_width">fill_parent</item> 
    </style> 

    <style name="TextViewStandard" parent="@android:style/Widget.TextView"> 
     <item name="android:layout_height">wrap_content</item> 
     <item name="android:layout_marginLeft">2dip</item> 
     <item name="android:layout_marginRight">2dip</item> 
     <item name="android:layout_marginTop">2dip</item> 
     <item name="android:layout_width">fill_parent</item> 
     <item name="android:textColor">@android:color/white</item> 
    </style> 
</resources> 

này trở nên thực sự sai lầm với một CheckBox có liên quan.

Có gì sai với định nghĩa của tôi?

Rất cám ơn trước. HJW

Trả lời

60

Bạn đã cố gắng để thiết lập các thuộc tính android:layout_width-0dp (và dĩ nhiên giữ android:layout_weight như 1) trên quan điểm con?

Tôi đã ở trong tình huống tương tự khi tôi muốn phân phối không gian đã cho bằng nhau giữa hai lượt xem con nhưng không làm như vậy vì trẻ có "nội dung rộng hơn" cũng trở nên rộng hơn, bên trong bố mẹ của nó. Điều này là do thực tế là đứa trẻ rộng hơn có chiều rộng ban đầu lớn hơn. Đảm bảo cả hai trẻ em bắt đầu từ cùng một chiều rộng (android:layout_width="0dp" trên cả hai người trong số họ) cũng được bảo đảm để phân phối không gian đồng đều giữa chúng.

+11

Quan trọng hơn, nếu bạn làm điều này, hãy đổ 'TableLayout' và chuyển sang' LinearLayout'. 'TableLayout' được thiết kế cho các trường hợp bạn cần Android để tính toán độ rộng cột dựa trên nội dung ô. Nếu bạn cố ý bỏ qua điều đó, bạn cũng có thể sử dụng 'LinearLayouts' lồng nhau thay thế. – CommonsWare

+0

@ChristianB: Cảm ơn bạn đã giải thích. Tôi luôn nghĩ rằng phải có một cái gì đó "sai" với TableLayout. Điều thú vị là nhiều TableLayouts không hoạt động hoàn hảo với trình giả lập 2.3. @dbm: Với layout_width đặt thành 0dp tất cả các chế độ xem con (TextView/EditText) biến mất. Dù sao cũng cảm ơn bạn. Tôi sẽ kiểm tra giải pháp của CommonsWares và thả hoàn toàn TableLayouts. Không biết những gì họ là tốt cho nếu bạn cần phải biết những gì có thể được in trong họ và có thể mess chúng lên ... –

+2

@CommonsWare: Không thể tin tại sao tôi đã TableLayouts? Thay đổi chúng thành LinearLayout hoạt động hoàn hảo. Cảm ơn nhiều. –

0

Các cột trong bảng android luôn điều chỉnh theo chế độ xem rộng nhất trong bất kỳ hàng nào. Không có điều khiển rõ ràng ở đây của các cột. Chế độ xem lưới có nhiều quyền kiểm soát hơn đối với kích thước cột như: android: columnWidth = "100dp"

Nếu chế độ xem trái và righ trong bảng sẽ được đặt thành cùng kích thước thì bảng căn giữa có thể có hai cột khoảng cách 50% .

17

Tôi sẽ tạo bố cục bảng Bên trong bố cục khung giữ quyền kiểm soát của bạn. Trong ví dụ này, tôi có hai cột bố trí và tôi đặt hai ImageView của trung tâm trong mỗi cột.

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

     <TableRow> 
      <FrameLayout 
       android:layout_height="wrap_content" 
       android:layout_width="0dp" 
       android:layout_weight="1"> 

       <ImageView android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/button_wedding_day_cheat_sheet" 
        android:id="@+id/buttonWeddingDayCheatSheet" 
        android:onClick="onClickWeddingDayCheatSheet" 
        android:layout_gravity="center_horizontal"> 
       </ImageView> 
      </FrameLayout> 

      <FrameLayout 
       android:layout_height="wrap_content" 
       android:layout_width="0dp" 
       android:layout_weight="1"> 
       <ImageView android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/button_share_favorite_recipe" 
        android:id="@+id/buttonShareFavoriteRecipe" 
        android:onClick="onClickShareFavoriteRecipe" 
        android:layout_gravity="center_horizontal"> 
       </ImageView> 
       </FrameLayout> 
      </TableRow> 

    </TableLayout> 
+2

Cảm ơn bạn thân !!!! Nó giúp tôi rất nhiều ... – krisDrOid

+1

Điều này làm việc cho tôi. Tôi đã phải thêm center_vertical vào các phần tử FrameLayout. – Steven

24

Đây là cách tôi định dạng bảng một cách đồng đều. Không phải là một phần của câu hỏi của bạn, nhưng để kéo dài tất cả các cột android: layout_span = "2" trên các trẻ em TableRow khác không chứa thuộc tính android: layout_column. Hi vọng điêu nay co ich.

<TableLayout 
    android:id="@+id/tableLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="0,1"> 

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

     <TextView 
      android:id="@+id/textView1" 
      android:layout_column="0"> 
     </TextView> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_column="1"> 
     </TextView> 

    </TableRow> 

</TableLayout> 
Các vấn đề liên quan