【问题标题】:Programatically add view to match parent to fill TableLayout以编程方式添加视图以匹配父项以填充 TableLayout
【发布时间】:2015-10-05 15:57:54
【问题描述】:

我正在尝试动态创建一个视图以充当表格行之间的分隔符,但是即使我试图让它们与父级匹配,我的视图也只会与文本一样宽。

我正在使用

动态添加到 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>

【问题讨论】:

    标签: android android-tablelayout


    【解决方案1】:

    滚动视图的宽度是 Wrap Content...试试 Match Parent

    【讨论】:

    • 不幸的是没有任何影响
    猜你喜欢
    • 2012-03-29
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多