android - LayoutInflater View 未拉伸(stretch)到 match_parent

标签 android android-layout tablelayout tablerow

我很难获得正在膨胀以匹配父级宽度的 View 。我在 Activity 的 setContentView 上使用的布局包含以下内容

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"    
    android:scrollbars="none" >

    <TableLayout
        android:id="@+id/tableRequestContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </TableLayout>

</ScrollView>

我正在膨胀的布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layoutRequestItemContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/Blue">

<TextView
    android:id="@+id/RequestItemUnit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:paddingLeft="10dp" />

<TextView
    android:id="@+id/lblRequestItemCost"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"        
    android:textSize="16sp" />

</LinearLayout>

然后在我的 Activity 中,我遍历一个包含我的数据的 ArrayList 并在每次迭代中扩充 View ,创建一个新的 TableRow,将 View 添加到 TableRow,然后将 TableRow 添加到父表。我在下面使用的代码:

public class Requests extends Activity {
    View view;
    LayoutInflater inflater;        
    RequestData requestData;
    TableLayout requestTable

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         setContentView(R.layout.request_layout);
         requestTable = (TableLayout) findViewById(R.id.tableRequestContainer);
         // HERE I POPULATE THE DATA OBJECT requestData with values
         requestData = getRequestData();
         for (int x=0; x<requestData.size(); x++) {
             TableRow tr = new TableRow(this);
             tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
             view = LayoutInflater.from(this).inflate(R.layout.request_item_layout, tr, false);
             // HERE IS THEN FIND THE TEXTVIEWS using view.findViewById(R...) and setText on each to the value in my data object.  This works fine
             // Add the view to the table row
             tr.addView(view);
             // Add the tablerow to the table
             requestTable.addView(tr);
         }
    }
}

问题是 View 正确膨胀并且我能够设置 TextView 的值,但是容器使用 wrap_content 作为宽度而不是我在 XML 中指定的 match_parent。我不知道为什么,并且一直在尝试我能想到的一切,但仍然无法获得膨胀 View 以将宽度拉伸(stretch)到 match_parent。目标是我膨胀的 View 应该是用户屏幕的整个宽度。

非常感谢任何帮助!谢谢!!!

最佳答案

如果 Set Layoutparams 不起作用,您可以尝试以下操作:

检查是否动态设置了TableLayout的如下属性:

tableLayout.setStretchAllColumns(true);

关于android - LayoutInflater View 未拉伸(stretch)到 match_parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28835423/

相关文章:

android - 在 Android 中创建自定义 ROM

android - 自定义 View 不绘制自己

Android 不使用设备宽度和高度创建运行时 View

Android - 向布局添加 View

android - 如何在表格布局中的一行的同一列中添加两个按钮?

android - 表格布局上的按钮缩放

android - Android Studio 3.1.1更新后, 'locate'链接打不开生成的APK文件夹

android - Gradle 错误 : Failed to resolve: com. google.firebase :firebase-core:16. 0.1

android - 为扩展包创建 0% 压缩的 zip 文件 mac

android - 表布局分离