以编程方式添加视图以匹配父级以填充TableLayout

时间:2015-10-05 15:57:54

标签: android android-tablelayout

我正在尝试动态创建一个View作为表行之间的间隔,但是我的视图只会像文本一样宽,即使我试图让它们与父类匹配。

enter image description here

我正在使用

动态添加到TableLayout
TableRow tr = new TableRow(this);    

tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
tr.setOrientation(LinearLayout.VERTICAL);
tr.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

layout.setPadding(15, 15, 10, 15);

TextView description = new TextView(this);
description.setText(row.getString(TAG_DESCRIPTION));
description.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
description.setTextSize(25);

TextView dateCreated = new TextView(this);
dateCreated.setText("Date Created: " + row.getString(TAG_DATEADDED).replace("\\", ""));
dateCreated.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
dateCreated.setTextSize(20);

TextView reportType = new TextView(this);
reportType.setText("Report Type: " + row.getString(TAG_REPTYPE));
reportType.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
reportType.setTextSize(20);

View view = new View(this);
view.setMinimumHeight(2);
view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

layout.addView(description);
layout.addView(dateCreated);
layout.addView(reportType);
layout.addView(view);

tr.addView(layout);

existingReports.addView(tr);

我用

在我的XML中定义了我的TableLayout
<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/txt_numExistingReports"
    android:layout_alignStart="@+id/txt_numExistingReports"
    android:layout_below="@+id/txt_numExistingReports"
    android:layout_alignParentBottom="true"
    android:background="@android:color/white"
    android:layout_toLeftOf="@+id/home"
    android:layout_toStartOf="@+id/home">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="30dp"
        android:showDividers="middle"
        android:id="@+id/existingReports"
        android:layout_marginTop="5dp"
        android:divider="?android:attr/dividerHorizontal"
        android:background="@android:color/white"/>
</ScrollView>

1 个答案:

答案 0 :(得分:0)

滚动视图宽度为自动换行内容...尝试匹配父级