【问题标题】:Static buttons under a ScrollView with a dynamic tablelayout within itScrollView 下的静态按钮,其中包含动态表格布局
【发布时间】:2018-11-16 21:43:12
【问题描述】:

我有一个动态表,它可以少至 2 行,但也可以有多达 50 行。我正在尝试将该表包装在滚动视图中。我还需要在页面底部显示 2 个按钮。

这是我正在努力实现的目标,但遇到了困难(附上图片以显示我正在尝试的目标):

  1. 如果行数很少,则不应出现滚动视图,并且表格应显示在表格下方,并且带有按钮。
  2. 如果行数超过 x,则将该表包装在具有最大高度的滚动视图中,然后在底部的静态位置显示按钮。表格滚动,但按钮在滚动视图下方消失。

有人可以建议我做错了什么吗?我在 StackOverflow 上浏览了很多问题,但找不到能解决这种特殊情况的问题。

下面是我的布局文件,我在活动中填充表格:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/sv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableLayout android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:id="@+id/mainTable">
        </TableLayout>
    </ScrollView>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="425dp"
            android:layout_gravity="center_horizontal"
            android:text="Submit" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center_horizontal"
            android:text="Cancel" />
    </LinearLayout>
</LinearLayout>

【问题讨论】:

    标签: android android-layout scrollview


    【解决方案1】:

    我会考虑在ScrollView 中使用RecyclerView 而不是TableLayout。如果您可以为按钮视图布局设置静态高度,则可以操作填充和边距以获得所需的效果。请参阅下面从这个答案修改:https://stackoverflow.com/a/45161728/6723646

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <ScrollView
            android:id="@+id/sv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="45dp">
            <TableLayout android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:id="@+id/mainTable">
            </TableLayout>
        </ScrollView>
        <LinearLayout
            android:layout_marginTop="-45dp"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:orientation="horizontal"
            android:layout_gravity="center">
            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="Submit" />
    
            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center_horizontal"
                android:text="Cancel" />
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

    • 谢谢@airfish。我不确定如何将表格布局与 recyclerview 对齐并从活动中动态附加它。
    • 我想您可以使用相同的概念,将 TableLayout 嵌入到 ScrollView 中,而不是上面的 RecyclerViewScrollView 将具有与 RecyclerView 相同的属性,TableLayout 将保持不变。
    • 试过这个 - 一个问题 - 滚动条出现在第二列旁边,而不是在最右边。
    • 尝试使滚动视图的宽度与父级和表格布局相匹配,并且布局重心在父级中
    • 谢谢@airfish - 你成功了....这太棒了....只需要再提供一个帮助!无论我如何对齐屏幕 - 纵向或横向,我都需要底部的两个按钮对齐到彼此相邻的中心。
    【解决方案2】:

    感谢 @airfish 和 @Onik 提供的解决方案。经过两天的努力,@airfish 的解决方案终于解决了我的问题。

    这是最终的解决方案,任何有兴趣让它工作的人。

    最终布局文件。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <ScrollView
            android:id="@+id/sv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="45dp">
            <TableLayout android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:gravity="center_horizontal"
                android:id="@+id/mainTable">
            </TableLayout>
        </ScrollView>
        <LinearLayout
            android:layout_marginTop="-45dp"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:orientation="horizontal"
            android:layout_gravity="center">
            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="Submit" />
    
            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center_horizontal"
                android:text="Cancel" />
        </LinearLayout>
    </LinearLayout>
    

    MainActivity.java(编辑第 30 行以增加/减少测试行数)

    package com.example.scroll_ex;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            TableLayout mainTable = findViewById(R.id.mainTable);
    
            mainTable.removeAllViews();
    
            int leftRowMargin = 0;
            int topRowMargin = 0;
            int rightRowMargin = 0;
            int bottomRowMargin = 0;
            int textSize = 0, smallTextSize = 0, mediumTextSize = 0;
    
            TableRow tr = new TableRow(this);
            TextView column1 = new TextView(this);
            TextView column2 = new TextView(this);
    
            for(int i=-1; i < 25; i++){
    
                tr = new TableRow(this);
                column1 = new TextView(this);
                column2 = new TextView(this);
    
                tr.setGravity(Gravity.CENTER);
                tr.setId(i+ 1);
                TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                        TableLayout.LayoutParams.WRAP_CONTENT);
                trParams.setMargins(leftRowMargin, topRowMargin, rightRowMargin, bottomRowMargin);
                tr.setPadding(0,0,0,0);
                tr.setLayoutParams(trParams);
    
                if (i == -1) {
                    column1.setText("Name");
                    column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                            TableRow.LayoutParams.MATCH_PARENT));
                    column1.setPadding(5, 5, 1, 5);
                } else {
                    column1.setText("Name #" + i);
                    column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                            TableRow.LayoutParams.WRAP_CONTENT));
                    column1.setPadding(5, 0, 1, 5);
                }
    
                tr.addView(column1);
    
    
                if (i == -1) {
                    column2.setText("Address");
                    column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                            TableRow.LayoutParams.MATCH_PARENT));
                    column2.setPadding(5, 5, 1, 5);
                } else {
                    column2.setText("Address " + i);
                    column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                            TableRow.LayoutParams.WRAP_CONTENT));
                    column2.setPadding(5, 0, 1, 5);
                }
    
                tr.addView(column2);
                mainTable.addView(tr, trParams);
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      相关资源
      最近更新 更多