滚动浏览 tablayout 在 Android 中不起作用

发布于 2024-12-05 16:27:24 字数 4010 浏览 2 评论 0原文

我正在尝试使嵌套在相对视图中的表格布局可滚动。 尝试了几个教程,但没有任何效果。这是 xml 和我的 java 代码:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/white" android:padding="10px"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/textView1"
    android:paddingBottom="10dp" android:text="Mijn Rooster"
    android:textSize="20sp" android:layout_alignParentTop="true"
    android:layout_alignLeft="@+id/tablelayout"></TextView>
<ScrollView android:id="@+id/ScrollView01"
    android:layout_width="wrap_content" android:layout_height="wrap_content">
    <TableLayout android:id="@+id/tablelayout"
        android:layout_width=
"fill_parent" android:layout_height="wrap_content"
        android:stretchColumns="0" android:layout_below="@+id/textView1"
        android:layout_above="@+id/btnsearch">

    </TableLayout>
</ScrollView>

<Button android:id="@+id/btnsearch" android:layout_width="wrap_content"
    android:text="Zoek op Datum" android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"></Button>
</RelativeLayout>

这是 JAVA 代码:

TableLayout tl = (TableLayout) findViewById(R.id.tablelayout);

    if (date_selected == null) {
        for (int i = 0; i < roostermap.size(); i++) {

            TableRow trdaydatetime = new TableRow(this);
            TableRow trinfo = new TableRow(this);

            trdaydatetime.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams(
                    TableRow.LayoutParams.FILL_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            rowSpanLayout.span = 3;

            TextView txtDay = new TextView(this);
            TextView txtTime = new TextView(this);
            TextView txtResult = new TextView(this);

            // Set Text
            txtDay.setText(roostermap.get(i).getDay(
                    roostermap.get(i).getRoster_date().getDay(),
                    roostermap.get(i).getRoster_date())
                    + " " + df.format(roostermap.get(i).getRoster_date()));
            txtTime.setText(dft.format(roostermap.get(i).getRoster_start())
                    + " - " + dft.format(roostermap.get(i).getRoster_end()));
            txtResult.setText(roostermap.get(i).getWorkplace_name());

            // Day&Date Layout
            txtDay.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            txtDay.setPadding(5, 5, 0, 0);
            txtDay.setTextColor(Color.BLACK);

            // Text Time layout
            txtTime.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            txtTime.setTextColor(Color.BLACK);
            txtTime.setPadding(0, 5, 10, 0);
            txtTime.setGravity(Gravity.RIGHT);

            // Text Result layout
            txtResult.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            txtResult.setPadding(5, 0, 0, 5);

            trdaydatetime.addView(txtDay);
            trdaydatetime.setBackgroundResource(R.drawable.trup);
            trdaydatetime.addView(txtTime);
            trinfo.addView(txtResult, rowSpanLayout);
            trinfo.setBackgroundResource(R.drawable.trdown);

            tl.addView(trdaydatetime, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            tl.addView(trinfo, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        }

I'm trying to make my tablelayout which is nested in a relativeview scrollable.
Tried a couple of tutorials but nothing worked. Here is the xml and my java code:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/white" android:padding="10px"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/textView1"
    android:paddingBottom="10dp" android:text="Mijn Rooster"
    android:textSize="20sp" android:layout_alignParentTop="true"
    android:layout_alignLeft="@+id/tablelayout"></TextView>
<ScrollView android:id="@+id/ScrollView01"
    android:layout_width="wrap_content" android:layout_height="wrap_content">
    <TableLayout android:id="@+id/tablelayout"
        android:layout_width=
"fill_parent" android:layout_height="wrap_content"
        android:stretchColumns="0" android:layout_below="@+id/textView1"
        android:layout_above="@+id/btnsearch">

    </TableLayout>
</ScrollView>

<Button android:id="@+id/btnsearch" android:layout_width="wrap_content"
    android:text="Zoek op Datum" android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"></Button>
</RelativeLayout>

And here is the JAVA code:

TableLayout tl = (TableLayout) findViewById(R.id.tablelayout);

    if (date_selected == null) {
        for (int i = 0; i < roostermap.size(); i++) {

            TableRow trdaydatetime = new TableRow(this);
            TableRow trinfo = new TableRow(this);

            trdaydatetime.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams(
                    TableRow.LayoutParams.FILL_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            rowSpanLayout.span = 3;

            TextView txtDay = new TextView(this);
            TextView txtTime = new TextView(this);
            TextView txtResult = new TextView(this);

            // Set Text
            txtDay.setText(roostermap.get(i).getDay(
                    roostermap.get(i).getRoster_date().getDay(),
                    roostermap.get(i).getRoster_date())
                    + " " + df.format(roostermap.get(i).getRoster_date()));
            txtTime.setText(dft.format(roostermap.get(i).getRoster_start())
                    + " - " + dft.format(roostermap.get(i).getRoster_end()));
            txtResult.setText(roostermap.get(i).getWorkplace_name());

            // Day&Date Layout
            txtDay.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            txtDay.setPadding(5, 5, 0, 0);
            txtDay.setTextColor(Color.BLACK);

            // Text Time layout
            txtTime.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            txtTime.setTextColor(Color.BLACK);
            txtTime.setPadding(0, 5, 10, 0);
            txtTime.setGravity(Gravity.RIGHT);

            // Text Result layout
            txtResult.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            txtResult.setPadding(5, 0, 0, 5);

            trdaydatetime.addView(txtDay);
            trdaydatetime.setBackgroundResource(R.drawable.trup);
            trdaydatetime.addView(txtTime);
            trinfo.addView(txtResult, rowSpanLayout);
            trinfo.setBackgroundResource(R.drawable.trdown);

            tl.addView(trdaydatetime, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            tl.addView(trinfo, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

殊姿 2024-12-12 16:27:24

为了使“滚动”工作,您需要 ScrollView内容ScrollView 更大本身。由于您已将 android:layout_height 值设置为 wrap_content,因此 ScrollView 的高度与其内容的高度相匹配 - 这意味着不会滚动。

通过将 ScrollView 设置为 fill_parent(可能使用 android:layout_weight)来修复 ScrollViewandroid:layout_height 值或像素值。

In order for the 'scrolling' to work you need the content of the ScrollView to be larger than the ScrollView itself. Since you have set the android:layout_height value to wrap_content, the height of the ScrollView matches the height of its content - which means no scrolling.

Fix the android:layout_height value of the ScrollView by either setting it to fill_parent (possibly with android:layout_weight) or a pixel value.

老旧海报 2024-12-12 16:27:24

消除滚动视图。您不能组合两个可滚动的视图,尤其是在同一方向滚动的情况下。

如果内容大于 TableView 的高度,TableView 本身会处理滚动。

Eliminate the ScrollView. You cannot combine two Views which are both scrollable, especially if the scroll in the same direction.

A TableView itself handles scrolling if the content is larger then the height of the TableView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文