如何在 Table 布局中添加滚动视图?
How to add scrollview in Table Layout?
这是我第一次使用 Scrollview,我想制作具有 tablelayout 3x8 行的应用程序。行的高度和宽度相同,table 可以垂直滚动。我怎样才能在 xml 布局中做到这一点?感谢您的帮助
<ScrollView android:id="@+id/layout"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_marginTop="5dip"
android:scrollbarStyle="outsideInset"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
这是您必须 TableRow
手动设置高度的方法之一,因此您可以按如下方式实现此目的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView android:id="@+id/layout"
android:layout_height="match_parent"
android:scrollbars="horizontal|vertical"
android:layout_width="match_parent"
android:layout_marginTop="5dip"
android:scrollbarStyle="outsideInset"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent" android:orientation="vertical"
android:layout_height="match_parent">
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
</LinearLayout>
</ScrollView>
</LinearLayout>
您可以使用 GridView
或 RecyclerView
实现此目的的另一种方法。 RecyclerView
是实现此目的的最简单方法。我希望它对你有用。谢谢
从 ScrollView
中删除 LinearLayout
并尝试,如果它不起作用则使用此
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="200dp" >
<EditText
android:layout_weight="1"
android:background="#F00"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#FF0"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#F0F"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="100dp">
<EditText
android:layout_weight="1"
android:background="#F00"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#FF0"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#F0F"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="100dp">
<EditText
android:layout_weight="1"
android:background="#F00"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#FF0"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#F0F"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="100dp">
<EditText
android:layout_weight="1"
android:background="#F00"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#FF0"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#F0F"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="100dp">
<EditText
android:layout_weight="1"
android:background="#F00"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#FF0"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#F0F"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="100dp">
<EditText
android:layout_weight="1"
android:background="#F00"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#FF0"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#F0F"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
</TableLayout>
</ScrollView>
不要使用 relative layout
以外的任何布局。我不知道它是如何工作的,但它确实有效。[至少对我来说]
这是我第一次使用 Scrollview,我想制作具有 tablelayout 3x8 行的应用程序。行的高度和宽度相同,table 可以垂直滚动。我怎样才能在 xml 布局中做到这一点?感谢您的帮助
<ScrollView android:id="@+id/layout"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_marginTop="5dip"
android:scrollbarStyle="outsideInset"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center">
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
这是您必须 TableRow
手动设置高度的方法之一,因此您可以按如下方式实现此目的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView android:id="@+id/layout"
android:layout_height="match_parent"
android:scrollbars="horizontal|vertical"
android:layout_width="match_parent"
android:layout_marginTop="5dip"
android:scrollbarStyle="outsideInset"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent" android:orientation="vertical"
android:layout_height="match_parent">
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</TableRow>
</LinearLayout>
</ScrollView>
</LinearLayout>
您可以使用 GridView
或 RecyclerView
实现此目的的另一种方法。 RecyclerView
是实现此目的的最简单方法。我希望它对你有用。谢谢
从 ScrollView
中删除 LinearLayout
并尝试,如果它不起作用则使用此
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="200dp" >
<EditText
android:layout_weight="1"
android:background="#F00"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#FF0"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#F0F"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="100dp">
<EditText
android:layout_weight="1"
android:background="#F00"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#FF0"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#F0F"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="100dp">
<EditText
android:layout_weight="1"
android:background="#F00"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#FF0"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#F0F"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="100dp">
<EditText
android:layout_weight="1"
android:background="#F00"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#FF0"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#F0F"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="100dp">
<EditText
android:layout_weight="1"
android:background="#F00"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#FF0"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#F0F"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="100dp">
<EditText
android:layout_weight="1"
android:background="#F00"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#FF0"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:layout_weight="1"
android:background="#F0F"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
</TableLayout>
</ScrollView>
不要使用 relative layout
以外的任何布局。我不知道它是如何工作的,但它确实有效。[至少对我来说]