如何使动态生成的表格布局可滚动?

我在 Activity 中有 3 个片段,在其中两个片段中我在 a 中有硬编码TableLayout,ConstraintLayout表的标题和行是使用来自 MySQL 服务器的信息动态生成的。鉴于其动态特性,表格能够溢出页面,我希望它可以滚动。


StackOverflow 上已经有多个问题,人们想要一个可滚动的表格,但在这些问题中,表格及其数据是硬编码的(不知道这是否相关)。我已经尝试了这些问题中提出的解决方案,但它们在我的桌子上不起作用。解决方案通常是将TableLayouta包装起来ScrollView或使用这两种方法android:isScrollContainer="1"都不适合我。


TableLayout里面ScrollView。用于测试,ScrollView通常它只是TableLayout.


<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".ViewProductLocFragment">

    <!-- Rest of the fragment -->


    <ScrollView

        android:layout_width="0dp"

        android:layout_height="0dp"

        android:layout_marginStart="8dp"

        android:layout_marginTop="16dp"

        android:layout_marginEnd="8dp"

        android:layout_marginBottom="16dp"

        android:layout_weight="1"

        android:scrollbars="none"

        app:layout_constraintBottom_toTopOf="@+id/menuBar"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/view">


        <TableLayout

            android:id="@+id/tableLayout"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginStart="5dp"

            android:layout_marginTop="5dp"

            android:layout_marginEnd="5dp"

            android:layout_marginBottom="5dp"

            android:stretchColumns="*"

            app:layout_constraintBottom_toTopOf="@+id/menuBar"

            app:layout_constraintEnd_toEndOf="parent"

            app:layout_constraintStart_toStartOf="parent"

            app:layout_constraintTop_toBottomOf="@+id/view">

        </TableLayout>

    </ScrollView>


</android.support.constraint.ConstraintLayout>


繁华开满天机
浏览 114回答 2
2回答

噜噜哒

您可以使用TableView 库使您的表格可滚动使用动态数据。链接如下:-https://github.com/evrencoskun/TableView

月关宝盒

我已经尝试通过 Activity 实现您的代码。我发布了我的 Activity 和 XML 代码供您参考,您能否查看完整代码中缺少的内容,以便提供帮助。MainActivity.javapublic class MainActivity extends Activity {TableLayout tableLayout;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; initializeTableLayout();}@Overrideprotected void onResume() {&nbsp; &nbsp; super.onResume();&nbsp; &nbsp; fillTable();}private void fillTable() {&nbsp; &nbsp; Integer count = 0;&nbsp; &nbsp; for (int i = 0; i < 100; i++) {&nbsp; &nbsp; &nbsp; &nbsp; String barcode = "#0000000000";&nbsp; &nbsp; &nbsp; &nbsp; String location = "Location";&nbsp; &nbsp; &nbsp; &nbsp; Integer quantity = 1;&nbsp; &nbsp; &nbsp; &nbsp; TableRow tableRow = new TableRow(MainActivity.this);&nbsp; &nbsp; &nbsp; &nbsp; if (count % 2 != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tableRow.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; tableRow.setId(View.generateViewId());&nbsp; &nbsp; &nbsp; &nbsp; tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));&nbsp; &nbsp; &nbsp; &nbsp; TextView labelBarcode = new TextView(MainActivity.this);&nbsp; &nbsp; &nbsp; &nbsp; labelBarcode.setId(View.generateViewId());&nbsp; &nbsp; &nbsp; &nbsp; labelBarcode.setGravity(Gravity.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; labelBarcode.setTextColor(getResources().getColor(R.color.colorAccent));&nbsp; &nbsp; &nbsp; &nbsp; labelBarcode.setTextSize(18);&nbsp; &nbsp; &nbsp; &nbsp; labelBarcode.setText(barcode);&nbsp; &nbsp; &nbsp; &nbsp; tableRow.addView(labelBarcode);&nbsp; &nbsp; &nbsp; &nbsp; TextView labelLocation = new TextView(MainActivity.this);&nbsp; &nbsp; &nbsp; &nbsp; labelLocation.setId(View.generateViewId());&nbsp; &nbsp; &nbsp; &nbsp; labelLocation.setGravity(Gravity.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; labelLocation.setTextColor(getResources().getColor(R.color.colorAccent));&nbsp; &nbsp; &nbsp; &nbsp; labelLocation.setTextSize(18);&nbsp; &nbsp; &nbsp; &nbsp; labelLocation.setText(location);&nbsp; &nbsp; &nbsp; &nbsp; tableRow.addView(labelLocation);&nbsp; &nbsp; &nbsp; &nbsp; TextView labelQuantity = new TextView(MainActivity.this);&nbsp; &nbsp; &nbsp; &nbsp; labelQuantity.setId(View.generateViewId());&nbsp; &nbsp; &nbsp; &nbsp; labelQuantity.setGravity(Gravity.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; labelQuantity.setTextColor(getResources().getColor(R.color.colorAccent));&nbsp; &nbsp; &nbsp; &nbsp; labelQuantity.setTextSize(18);&nbsp; &nbsp; &nbsp; &nbsp; labelQuantity.setText(quantity.toString());&nbsp; &nbsp; &nbsp; &nbsp; tableRow.addView(labelQuantity);&nbsp; &nbsp; &nbsp; &nbsp; tableLayout.addView(tableRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));&nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; }}private void initializeTableLayout() {&nbsp; &nbsp; tableLayout = this.findViewById(R.id.tableLayout);&nbsp; &nbsp; TableRow tr_head = new TableRow(MainActivity.this);&nbsp; &nbsp; tr_head.setId(View.generateViewId());&nbsp; &nbsp; tr_head.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));&nbsp; &nbsp; tr_head.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));&nbsp; &nbsp; TextView label_barcode = new TextView(MainActivity.this);&nbsp; &nbsp; label_barcode.setId(View.generateViewId());&nbsp; &nbsp; label_barcode.setText("BARCODE");&nbsp; &nbsp; label_barcode.setTextSize(20);&nbsp; &nbsp; label_barcode.setTextColor(Color.BLACK);&nbsp; &nbsp; label_barcode.setGravity(Gravity.CENTER);&nbsp; &nbsp; tr_head.addView(label_barcode);// add the column to the table row here&nbsp; &nbsp; TextView label_location = new TextView(MainActivity.this);&nbsp; &nbsp; label_location.setId(View.generateViewId());// define id that must be&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;unique&nbsp; &nbsp; label_location.setText("LOCATION"); // set the text for the header&nbsp; &nbsp; label_location.setTextSize(20);&nbsp; &nbsp; label_location.setTextColor(Color.BLACK); // set the color&nbsp; &nbsp; label_location.setGravity(Gravity.CENTER);&nbsp; &nbsp; tr_head.addView(label_location); // add the column to the table row here&nbsp; &nbsp; TextView label_quantity = new TextView(MainActivity.this);&nbsp; &nbsp; label_quantity.setId(View.generateViewId());// define id that must be&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;unique&nbsp; &nbsp; label_quantity.setText("QTY"); // set the text for the header&nbsp; &nbsp; label_quantity.setTextSize(20);&nbsp; &nbsp; label_quantity.setTextColor(Color.BLACK); // set the color&nbsp; &nbsp; label_quantity.setGravity(Gravity.CENTER);&nbsp; &nbsp; tr_head.addView(label_quantity); // add the column to the table row here&nbsp; &nbsp; tableLayout.setScrollContainer(true);&nbsp; &nbsp; tableLayout.addView(tr_head, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));}}activity_main.xml&nbsp; &nbsp; <?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; tools:context=".MainActivity">&nbsp; &nbsp; <ScrollView&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginStart="8dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="16dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginEnd="8dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginBottom="16dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_weight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:scrollbars="none">&nbsp; &nbsp; &nbsp; &nbsp; <TableLayout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/tableLayout"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginStart="5dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="5dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginEnd="5dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginBottom="5dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:stretchColumns="*"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"></TableLayout>&nbsp; &nbsp; </ScrollView></android.support.constraint.ConstraintLayout>我手动添加了 100 个虚拟行来测试它的滚动行为,并想通知您它完全可以正常工作。您的 Activity 或 Fragment 父代码中可能存在阻止表格滚动的问题。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java