gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:40:51 26 4
gpt4 key购买 nike

我很难获得正在膨胀以匹配父级宽度的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com