动态添加的tablerow只有最后一行不可见

时间:2016-08-18 10:50:37

标签: android android-tablelayout tablerow

我创建了一个tablelayout,在XML文件中添加了标题行,其余内容行动态填充。 出于某种原因,最后一行不可见。 我查看了其他相关帖子,我的代码中的一切似乎都没问题。 感谢有人可以看看并指出问题。

XML代码:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.bluechipsys.mcpackagemanager.PMActivity"
    android:id="@+id/view_layout">

    <ProgressBar
        android:id="@+id/installProgressBar"
        android:indeterminate="true"
        style="@android:style/Widget.Holo.ProgressBar.Large"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ProgressBar>

    <ScrollView
        android:id="@+id/textAreaScroller"
        android:layout_above="@+id/btn_reload"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:scrollbars="vertical"
        android:fadeScrollbars="false"
        android:singleLine="false">
    <TextView
        android:text="@string/msg_default"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:id="@+id/viewLog"
        android:maxLines="1000"
        android:scrollHorizontally="false"
        android:scrollbars="vertical"
        android:singleLine="false"
        android:background="@drawable/border" />
    </ScrollView>

    <TextView
        android:id="@+id/versionInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tableLayoutScroller"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/tableLayoutScroller"
        android:text="@string/tv_versioninfo"
        android:textStyle="bold" />

    <ScrollView
        android:id="@+id/tableLayoutScroller"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="false"
        android:layout_alignParentRight="false"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="false"
        android:layout_centerHorizontal="false"
        android:layout_centerInParent="false"
        android:layout_alignParentStart="@+id/textAreaScroller"
        android:layout_alignRight="@+id/textAreaScroller"
        android:layout_alignEnd="@+id/textAreaScroller"
        android:layout_above="@+id/textAreaScroller"
        android:fadeScrollbars="false"
        android:scrollbars="vertical">

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="vertical"
        android:id="@+id/tbl_packagelist"
        android:orientation="vertical"
        android:layout_alignParentLeft="false"
        android:layout_alignParentRight="false"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="false"
        android:layout_centerHorizontal="false"
        android:layout_centerInParent="false"
        android:layout_marginTop="30dp"
        android:layout_alignParentStart="@+id/textAreaScroller"
        android:layout_alignRight="@+id/textAreaScroller"
        android:layout_alignEnd="@+id/textAreaScroller"
        android:layout_above="@+id/textAreaScroller"
        android:scrollHorizontally="false"
        android:stretchColumns="1,2,3,4,5"
        android:shrinkColumns="2"
        android:background="@drawable/border_table">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:focusableInTouchMode="false"
            android:background="@android:drawable/list_selector_background">

            <CheckBox
                android:id="@+id/cbSelect"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/title_cell_shape"
                android:onClick="onCheckboxClicked" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/tvState"
                android:background="@drawable/title_cell_shape"
                android:editable="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/tv_package"
                android:id="@+id/tvPackage"
                android:background="@drawable/title_cell_shape"
                android:textColor="#000"
                android:textStyle="bold"
                android:gravity="fill"
                android:textAlignment="center"
                android:paddingLeft="2dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/tv_instver"
                android:id="@+id/tvInstver"
                android:singleLine="false"
                android:background="@drawable/title_cell_shape"
                android:textColor="#000"
                android:textStyle="bold"
                android:gravity="fill"
                android:textAlignment="center"
                android:paddingLeft="2dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/tv_latestver"
                android:id="@+id/tvLatestver"
                android:background="@drawable/title_cell_shape"
                android:textColor="#000"
                android:textStyle="bold"
                android:gravity="fill"
                android:paddingLeft="2dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/tv_size"
                android:id="@+id/tvSize"
                android:background="@drawable/title_cell_shape"
                android:textColor="#000"
                android:textStyle="bold"
                android:gravity="fill"
                android:paddingLeft="2dp" />

        </TableRow>
    </TableLayout>
    </ScrollView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_reload"
        android:id="@+id/btn_reload"
        android:background="@android:color/transparent"
        android:drawableTop="@drawable/reload"
        android:layout_marginTop="7dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_apply"
        android:id="@+id/btn_apply"
        android:background="@android:color/transparent"
        android:drawableTop="@drawable/apply"
        android:layout_alignTop="@+id/btn_reload"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_close"
        android:id="@+id/btn_close"
        android:background="@android:color/transparent"
        android:drawableTop="@drawable/close"
        android:layout_alignTop="@+id/btn_reload"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/textAreaScroller"
        android:layout_alignEnd="@+id/textAreaScroller" />

</RelativeLayout>

Java代码:

private void clearTableRows() {
        Logger.d(TAG, "Clearing table rows");

        TableLayout main_table = (TableLayout) findViewById(R.id.tbl_packagelist);
        int rowCount = main_table.getChildCount();
        main_table.removeViews(1, rowCount - 1);    // Leave header row intact
    }

    // Create table rows and add to the GUI
    private void createTableRows() {
        Logger.d(TAG, "Creating table rows");

        final TableLayout main_table = (TableLayout) findViewById(R.id.tbl_packagelist);
        Drawable background;

        // Define layout parameters
        TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        TableRow.LayoutParams itemCBParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        TableRow.LayoutParams itemIVParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        itemIVParams.gravity = Gravity.CENTER;      // Centre the image display
        TableRow.LayoutParams itemParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        ViewGroup.MarginLayoutParams mLayoutItemParams = new ViewGroup.MarginLayoutParams (itemParams);
        MarginLayoutParamsCompat.setMarginStart(mLayoutItemParams, 4);  // Need this workaround for API level < 17

        for (MCPackage pkg: list_packages) {
            // Create the TableRow and widgets to add to the TableRow
            TableRow tbl_row = new TableRow(this);
            CheckBox cb_select = new CheckBox(this);
            ImageView iv_state = new ImageView(this);
            TextView tv_package = new TextView(this);
            TextView tv_instver = new TextView(this);
            TextView tv_currver = new TextView(this);
            TextView tv_pkgsize = new TextView(this);

            int index = list_packages.indexOf(pkg);
            if(index % 2 == 0) {
                background = getResources().getDrawable(R.drawable.cell_shape_white);
            } else {
                background = getResources().getDrawable(R.drawable.cell_shape_grey);
            }

            // Set GUI parameters for the TableRow
            tbl_row.setLayoutParams(rowParams);
            tbl_row.setBackground(background);
            tbl_row.setId(index);           // Set ID to match index of list_packages

            // Initialize the cells
            cb_select.setLayoutParams(itemCBParams);
            cb_select.setId(index + CHECKBOX_ID);
            cb_ids[index] = (index + CHECKBOX_ID);
            cb_select.setOnClickListener(checkboxOnClick());
            cb_select.setOnCheckedChangeListener(checkboxOnCheckChange());

            iv_state.setLayoutParams(itemIVParams);
            iv_state.setImageResource(pkg.getState().getImageResourceId());
            iv_state.setId(index + IVSTATE_ID);
            iv_state_ids[index] = (index + IVSTATE_ID);

            tv_package.setLayoutParams(itemParams);
            tv_package.setText(pkg.getPackageName());
            tv_package.setGravity(Gravity.FILL);
            tv_package.setTextAppearance(this, android.R.style.TextAppearance_Small);
            tv_package.setTextSize(12);
            tv_package.setTextColor(Color.DKGRAY);
            tv_package.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

            tv_instver.setLayoutParams(itemParams);
            tv_instver.setText(pkg.getInstalledVersion());
            tv_instver.setGravity(Gravity.FILL);
            tv_instver.setTextSize(12);
            tv_instver.setTextColor(Color.BLUE);
            tv_instver.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

            tv_currver.setLayoutParams(itemParams);
            tv_currver.setText(pkg.getUpdateVersion());
            tv_currver.setGravity(Gravity.FILL);
            tv_currver.setTextSize(12);
            tv_currver.setTextColor(Color.DKGRAY);
            tv_currver.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

            tv_pkgsize.setLayoutParams(itemParams);
            tv_pkgsize.setText(String.valueOf(((int) pkg.getPackageSize()) / KBINBYTES) + "K");
            tv_pkgsize.setGravity(Gravity.FILL);
            tv_pkgsize.setTextSize(12);
            tv_pkgsize.setTextColor(Color.BLUE);
            tv_pkgsize.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

            // Add the cells to the row
            tbl_row.addView(cb_select);
            tbl_row.addView(iv_state);
            tbl_row.addView(tv_package);
            tbl_row.addView(tv_instver);
            tbl_row.addView(tv_currver);
            tbl_row.addView(tv_pkgsize);

            main_table.addView(tbl_row);        // Add the row to the table
            tbl_row.setOnClickListener(rowClickListener);
            tbl_row.setOnLongClickListener(rowLongClickListener);
            registerForContextMenu(tbl_row);    // Register the table row for context menu
            Logger.d(TAG, "Added package: " + pkg.getPackageName());
        }
    }

1 个答案:

答案 0 :(得分:-1)

我不确定这是否仍然是问题,但请尝试使用NestedScrollView而不是ScrollView