2017-10-04 90 views
2

我有一個xml佈局作爲ListView中的項目。事情是:當它被添加到那裏時,該項目的寬度不會延伸到match_parent,而是包裝內容。它用在ListView中,所以它的寬度需要是match_parent。我一直在閱讀關於它所有的stackoverflow,嘗試每個解決方案availible,但我有零結果:layout_weight設置爲1對我的佈局沒有任何影響,拉伸列給了我在寬度方面最好的結果,但在佈局本身它慘敗。TableLayout拒絕match_parent

這裏有什麼可以做的嗎?

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:descendantFocusability="blocksDescendants"> 

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

     <CheckBox 
      android:id="@+id/trackCheckBox" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 
      android:clickable="false" 
      android:duplicateParentState="true" /> 

     <TextView 
      android:id="@+id/trackNumberText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="1" 
      android:text="00." 
      android:textColor="@android:color/black" 
      android:textSize="18sp" /> 

     <TextView 
      android:id="@+id/trackTitleText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_column="2" 
      android:layout_weight="1" 
      android:text="Title" 
      android:textColor="@android:color/black" 
      android:textSize="18sp" /> 
    </TableRow> 

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

     <TextView 
      android:id="@+id/trackArtistText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_column="2" 
      android:layout_weight="1" 
      android:text="Artist" /> 
    </TableRow> 
</TableLayout> 
+0

listview在哪裏?和上面的XML你使用的ListView的孩子? – Ankita

回答

0

上述解決方案都沒有幫助我,所以試圖解決這個問題3天我自己找到了解決方案。不幸的是,該解決方案是編程的,下面的代碼將被添加到您的自定義適配器中:

trackCheckBox.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
trackNumberText.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
int width = context.getResources().getDisplayMetrics().widthPixels - trackCheckBox.getMeasuredWidth() - trackNumberText.getMeasuredWidth(); 

trackArtistText.setWidth(width); 
trackTitleText.setWidth(width); 
0

我想你想要的是tableLayout.setStretchAllColumns(true);。不幸的是,你不能在xml中設置它,也許把它放在你的適配器中。

1

添加android:stretchColumns="*"財產在你TableLayout

安卓stretchColumns

列的從零開始的索引伸展。列索引 必須用逗號分隔:1,2,5。忽略非法和重複索引 。您可以使用值「*」 來代替擴展所有列。請注意,一列可以同時標記爲可伸縮和可收縮的 。

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="*" 
    android:descendantFocusability="blocksDescendants"> 

..... 

</TableLayout> 

希望這會有所幫助。

+1

噢好吧...我不知道你可以設置它xml ... *營救 – meeeee

+0

對不起,在帖子中我說我已經試過這個。它沒有導致所需的佈局,儘管它將佈局寬度設置爲match_parent。你應該自己看看我試圖通過將這些代碼複製到你的android studio中,然後將拉伸列參數添加到代碼來查看實際發生的事情 –