动态地向TableLayout添加自定义视图

时间:2017-12-26 18:54:13

标签: android android-fragments view tablelayout

我试图动态地向TableLayout添加自定义视图。添加像TextView这样的东西可以工作但是当我试图给自己充气时,我会看到一个空白的屏幕。 我看到了类似的问题,但没有一个有任何帮助 我有一个膨胀成片段的布局:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/gameTableLayout"
             android:layout_width="match_parent"
             android:layout_height="match_parent">
</TableLayout>

然后我在Fragment中动态地将行添加到此表中:

@Override
public void onResume() {
    super.onResume();
    LayoutInflater li = getLayoutInflater();
    tableLayout = getActivity().findViewById(R.id.gameTableLayout);
    TableRow row = new TableRow(getContext());
    TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
            TableLayout.LayoutParams.MATCH_PARENT,
            TableLayout.LayoutParams.MATCH_PARENT);
    row.setLayoutParams(trParams);
    for (int i =0; i <3; i++) {
        View cardView = li.inflate(R.layout.card_covered_layout, null);
        row.addView(cardView);
    }
    tableLayout.addView(row);
}

这是R.layout.card_covered_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/cardCoveredLayout"
              android:layout_width="50dp"
              android:layout_height="50dp"
              android:layout_margin="3dp"
              android:background="@drawable/card_covered_background"
              android:orientation="horizontal">
</LinearLayout>

这就是card_covered_background:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorPrimary"/>
    <stroke android:width="3dp" android:color= "@color/colorPrimaryDark"/>
    <corners android:radius="10dp"/>
</shape>

0 个答案:

没有答案