TableLayout拒绝match_parent

问题描述:

我有一个xml布局作为ListView中的项目。事情是:当它被添加到那里时,该项目的宽度不会延伸到match_parent,而是包装内容。它用在ListView中,所以它的宽度需要是match_parent。我一直在阅读关于它所有的*,尝试每个解决方案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

上述解决方案都没有帮助我,所以试图解决这个问题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); 

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

添加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中,然后将拉伸列参数添加到代码来查看实际发生的事情 –