2012-09-18 30 views
2

美好的一天,我有以下圖片的麻煩。正如你所看到的,表格行佔用了太多的空間,我想減少它們,因爲它們下面還有其他的視圖小部件。我怎麼能做到這一點?謝謝。TableRow需要比平常更多的空間android tablelaayout

enter image description here

這是我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <TableLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:weightSum="3" 
    android:padding="8dp" 
    android:background="@drawable/rounded_date_filter"> 

    <TableRow 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 

<TextView 
    android:id="@+id/datefilter_text_from_id" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:layout_weight="1" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="10dp" 
    android:text="@string/from_text"> 
</TextView> 
<TextView 
    android:id="@+id/datefilter_text_to_id" 
    android:layout_width="10dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:layout_marginLeft="40dp" 
    android:layout_marginTop="10dp" 
    android:text="01-01-2012"> 
</TextView> 
</TableRow> 

    <View 
     android:layout_width="fill_parent" 
     android:layout_height="1px" 
     android:background="#606060"> 
    </View> 

<TableRow> 
<TextView 
    android:id="@+id/datefilter_text_from_id" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:layout_weight="1" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="10dp" 
    android:text="@string/to_text"> 
</TextView> 

<TextView 
    android:id="@+id/datefilter_text_to_id" 
    android:layout_width="10dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:layout_marginLeft="40dp" 
    android:layout_marginTop="10dp" 
    android:text="01-09-2012"> 
</TextView> 
</TableRow> 

     <View 
     android:layout_width="fill_parent" 
     android:layout_height="1px" 
     android:background="#606060"> 
    </View> 

    <TableRow> 
<TextView 
    android:id="@+id/datefilter_text_from_id" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:layout_weight="1" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="10dp" 
    android:text="@string/groupby"> 
</TextView> 

<TextView 
    android:id="@+id/datefilter_text_to_id" 
    android:layout_width="10dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:layout_marginLeft="40dp" 
    android:layout_marginTop="10dp" 
    android:text="day"> 
</TextView> 
</TableRow> 

</TableLayout> 

    <Button 
     android:id="@+id/reset_filter_button_id" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/reset_filters"> 
    </Button> 
</LinearLayout> 
+0

從'TableRows'取出佈局重量。 – Luksprog

+0

@Luksprog對於遲到的回覆感到抱歉。我已經這樣做了,但它並沒有幫助 – irobotxxx

回答

1

我測試過你的佈局,最後看來weightSumlayout_weight不負責任。設置TableLayout以包裝內容和layout_weightTextViews似乎展開TableLayout並使其具有您看到的行爲。

如果設置的TableLayoutFILL_PARENT問題的寬度應消失:當你試圖使用的佈局重當父並沒有真正有空間騰出

// ... 
<TableLayout 
    android:layout_width="fill_parent" 
// ... 

這在某種程度上是有道理的(因爲wrap_content),沒有什麼好的可以來自這個。

而且,如果這是你所有的佈局,你可以改善它,並直接添加ButtonTableLayout(在另一個TableRow)消除對包裝LinearLayout的需要。

+0

感謝您的支持。您的解決方案有效我還發現我可以通過爲TextView設置maxHeight屬性來減少它,但是我認爲您的解決方案更清晰。另外我想要一個圓形的TableLayout,所以按鈕不能在TableLayout中。 – irobotxxx

+0

我可以使用這個機會詢問如何製作一個圓形的TableLayout嗎?..我知道你可以用形狀矩形和顏色定義一個drawable並將其用作背景。但我不想讓背景有任何顏色,我只想讓矩形的線條\邊框可見。任何想法如何做到這一點? – irobotxxx

+0

@sparrow我不確定自己明白。你想保留當前可用drawable作爲'TableLayout'的背景,並且在'TableLayout'周圍還有圓形的邊框嗎? – Luksprog

0

的layout_weight用來告訴佈局有力地填補空間。在上面的佈局中,TableRow的權重指定爲1,並將weightSum指定爲3.移除它將工作的layout_weight和weightSum。

+0

當我這樣做時它仍然不起作用。 – irobotxxx

0

這裏是校正佈局文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:padding="8dp" 
     android:weightSum="3" > 

     <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 

      <TextView 
       android:id="@+id/datefilter_text_from_id" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dp" 
       android:layout_marginTop="10dp" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:text="From" > 
      </TextView> 

      <TextView 
       android:id="@+id/datefilter_text_to_id" 
       android:layout_width="10dp" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="40dp" 
       android:layout_marginTop="10dp" 
       android:layout_weight="1" 
       android:text="01-01-2012" > 
      </TextView> 
     </TableRow> 

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

      <View 
       android:layout_width="fill_parent" 
       android:layout_height="1px" 
       android:layout_weight="1" 
       android:background="#606060" > 
      </View> 
     </TableRow> 

     <TableRow> 

      <TextView 
       android:id="@+id/datefilter_text_from_id" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dp" 
       android:layout_marginTop="10dp" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:text="To" > 
      </TextView> 

      <TextView 
       android:id="@+id/datefilter_text_to_id" 
       android:layout_width="10dp" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="40dp" 
       android:layout_marginTop="10dp" 
       android:layout_weight="1" 
       android:text="01-09-2012" > 
      </TextView> 
     </TableRow> 

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

      <View 
       android:layout_width="fill_parent" 
       android:layout_height="1px" 
       android:layout_weight="1" 
       android:background="#606060" > 
      </View> 
     </TableRow> 

     <TableRow> 

      <TextView 
       android:id="@+id/datefilter_text_from_id" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dp" 
       android:layout_marginTop="10dp" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:text="Group By" > 
      </TextView> 

      <TextView 
       android:id="@+id/datefilter_text_to_id" 
       android:layout_width="10dp" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="40dp" 
       android:layout_marginTop="10dp" 
       android:layout_weight="1" 
       android:text="day" > 
      </TextView> 
     </TableRow> 
    </TableLayout> 

    <Button 
     android:id="@+id/reset_filter_button_id" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="My Button" > 
    </Button> 

</LinearLayout> 

和輸出佈局:

enter image description here

我指出,並因此校正的問題是:

  • 添加了的row separation作爲一個單獨的行,而不是模棱兩可的視圖insi德TableLayout
  • 改變的TableLayoutlayout_widthwrap_contentfill_parent
相關問題