2013-02-23 103 views
2

我有一個滾動視圖表格佈局。它有2列。其中一個(第一個)是描述,第二個是數量。我已經這樣做了,如果有必要,第一列可以包裝文本。 我現在的問題是限制兩個欄的寬度,以便它們在屏幕上50-50%。並且第一列應該包裝。我曾嘗試使用android:minWidth =「215dp」,但我不想在xml中硬編碼我的值。表格佈局間距問題

這是xml。任何幫助表示讚賞。

<ScrollView 
    android:id="@+id/scroll" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_above="@+id/footer" 
    android:layout_below="@+id/tableheader" 
    android:fillViewport="true" 
    android:scrollbars="none" > 

    <TableLayout 
     android:id="@+id/dataTable" 
     style="@style/SummaryTableLayout" > 

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

     <TextView 
      android:gravity="left" 
      android:minWidth="215dp" 
      /> 

     <TextView 
      android:gravity="left" 
      android:minWidth="215dp" 
        /> 

     </TableRow> 
    </TableLayout> 
</ScrollView> 

的SummaryTableLayout風格的部分是

<style name="SummaryTableLayout" > 
    <item name="android:layout_width">fill_parent</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:stretchColumns">0</item> 
    <item name="android:shrinkColumns">0</item> 
    <item name="android:layout_marginTop">10dp</item> 
    <item name="android:layout_marginBottom">25dp</item> 
    <item name="android:scrollbars">vertical</item> 
    <item name="android:isScrollContainer">true</item> 
    <item name="android:scrollbarAlwaysDrawVerticalTrack">true</item> 
</style> 

編輯:::這是我個XML之一。我有第二個XML,其中表格佈局有4列,我希望這些與第一列有WRAPPING均勻間隔。

+0

嘗試對兩個文字視圖設置相等的權重 – Deepak 2013-02-23 09:45:43

回答

1

使用layout_weigth屬性。這應該適合你:

<TableLayout 
    android:id="@+id/dataTable" 
    style="@style/SummaryTableLayout" > 

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

    <TextView 
     android:gravity="left" 
     android:width="0px" 
     android:layout_weight="0.5"/> 

    <TextView 
     android:gravity="left" 
     android:width="0px" 
     android:layout_weight="0.5"/> 

    </TableRow> 
</TableLayout> 

按照這個答案herelayout_weight用於定義列的權限。

編輯請注意我取代layout_widthwidth爲列,看來這是在這種情況下使用正確的屬性。

+0

他可能想讓列僅佔用屏幕的50%,僅用於第一個「TextView」文本不小於屏幕一半的情況。 – Luksprog 2013-02-23 10:35:29

+0

@Luksprog:我可能誤解了他,但我自己寧願有固定的佈局,而不是根據內容更改列寬。 – 2013-02-23 10:36:45

+0

我同意你的意見,我只是做了一個小小的觀察。 – Luksprog 2013-02-23 10:39:32