滚动仅在屏幕中间不起作用

时间:2017-12-27 18:29:30

标签: java android xml android-layout

我正在开发一个购物应用程序。在一个活动中,我在表格布局中有一个项目列表。该表有一个标题行和两个页脚行。我想只在中间激活滚动。

要实现这一点,我有3个TableLayouts。 1只包含标题行。 1包含主项目列表,1包含页脚行。以下是我的代码。

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
    android:layout_height="fill_parent"
    android:layout_width="match_parent"
    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"
    tools:context="com.example.ahnaf.homemgt.Cart"
    android:columnCount="1"
    android:rowCount="5"
    >
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:background="@android:color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <Button
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:text="HomeMGT" />

    </android.support.design.widget.AppBarLayout>

    <TableLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:stretchColumns="0,1,2,3"
        >
        <TableRow
            android:minHeight="40dp">

            <Button
                android:id="@+id/addItem"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_span="4"
                android:background="@android:color/holo_blue_dark"
                android:minHeight="0dp"
                android:minWidth="0dp"
                android:onClick="addItem"
                android:text="Add Item To Cart"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        </TableRow>

    </TableLayout>

    <TableLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:stretchColumns="0,1,2,3"
        >
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_dark"
            android:gravity="center_horizontal">


            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:text="Name"
                android:textColor="@android:color/background_light"
                android:textAlignment="center"/>


            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:text="Qty"
                android:textColor="@android:color/background_light"
                android:textAlignment="center"/>

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:text="Price"
                android:textColor="@android:color/background_light"
                android:textAlignment="center"/>

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:text="Subtotal"
                android:textColor="@android:color/background_light"
                android:textAlignment="center"/>


        </TableRow>

    </TableLayout>


    <ScrollView>
        <TableLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:stretchColumns="0,1,2,3"
            android:id="@+id/myTableLayout">

        </TableLayout>
    </ScrollView>

    <TableLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:stretchColumns="0,1,2,3"
        android:layout_gravity="bottom"
        >
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_dark"
            android:gravity="center_horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:text="Total"
                android:textColor="@android:color/background_light"
                android:textAlignment="center"/>

            <TextView
                android:id="@+id/total"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="3"
                android:textColor="@android:color/background_light"
                android:textAlignment="center"/>


        </TableRow>

        <TableRow
            android:minHeight="40dp">

            <Button
                android:id="@+id/cashOut"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_span="4"
                android:background="@android:color/holo_blue_dark"
                android:minHeight="0dp"
                android:minWidth="0dp"
                android:onClick="addItem"
                android:text="Cash Out"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        </TableRow>
    </TableLayout>


</GridLayout>

最初一切看起来都不错,但是当我添加太多项目时,我的滚动不起作用。包含Cash out按钮和总textview的最后两行也会消失。我在这做错了什么?

*****************************解决以下代码************* ******

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

    <GridLayout
        android:layout_height="fill_parent"
        android:layout_width="match_parent"
        android:layout_below="@id/appBarLayout"
        android:columnCount="1"
        android:rowCount="5"
        >

        <TableLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:stretchColumns="0,1,2,3"
            >
            <TableRow
                android:minHeight="40dp">

                <Button
                    android:id="@+id/addItem"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_span="4"
                    android:background="@android:color/holo_blue_dark"
                    android:minHeight="0dp"
                    android:minWidth="0dp"
                    android:onClick="addItem"
                    android:text="Add Item To Cart"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

            </TableRow>

        </TableLayout>

        <TableLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:stretchColumns="0,1,2,3"
            >
            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/background_dark"
                android:gravity="center_horizontal">


                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="0"
                    android:text="Name"
                    android:textColor="@android:color/background_light"
                    android:textAlignment="center"/>


                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="1"
                    android:text="Qty"
                    android:textColor="@android:color/background_light"
                    android:textAlignment="center"/>

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="1"
                    android:text="Price"
                    android:textColor="@android:color/background_light"
                    android:textAlignment="center"/>

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="1"
                    android:text="Subtotal"
                    android:textColor="@android:color/background_light"
                    android:textAlignment="center"/>


            </TableRow>

        </TableLayout>


        <ScrollView
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
            <!--<LinearLayout-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="wrap_content"-->
                <!--android:orientation="vertical"-->
                <!--android:id="@+id/myLinearLayout">-->
                <TableLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:stretchColumns="0,1,2,3"
                    android:id="@+id/myTableLayout">

                </TableLayout>





            <!--</LinearLayout>-->
        </ScrollView>
    </GridLayout>

    <GridLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:columnCount="1"
        android:rowCount="5"
        >
        <TableLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:stretchColumns="0,1,2,3"
            android:layout_gravity="bottom"
            >
            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/background_dark"
                android:gravity="center_horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="2"
                    android:text="Total"
                    android:textColor="@android:color/background_light"
                    android:textAlignment="center"/>

                <TextView
                    android:id="@+id/total"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="3"
                    android:textColor="@android:color/background_light"
                    android:textAlignment="center"/>


            </TableRow>

            <TableRow
                android:minHeight="40dp">

                <Button
                    android:id="@+id/cashOut"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_span="4"
                    android:background="@android:color/holo_blue_dark"
                    android:minHeight="0dp"
                    android:minWidth="0dp"
                    android:onClick="addItem"
                    android:text="Cash Out"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

            </TableRow>
        </TableLayout>

    </GridLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

如果下面的代码适合您,请告诉我,我已将所有内容包装在相对布局中,然后将网格布局分为两部分,第一部分位于顶部,而另一部分锚定在屏幕底部。顶部gridview有滚动视图,可滚动,“页脚”是粘性的,只有中间部分滚动:

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <GridLayout
        android:layout_height="fill_parent"
        android:layout_width="match_parent"
        android:layout_below="@id/appBarLayout"
        android:columnCount="1"
        android:rowCount="5"
        >

    <TableLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:stretchColumns="0,1,2,3"
        >
        <TableRow
            android:minHeight="40dp">

            <Button
                android:id="@+id/addItem"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_span="4"
                android:background="@android:color/holo_blue_dark"
                android:minHeight="0dp"
                android:minWidth="0dp"
                android:onClick="addItem"
                android:text="Add Item To Cart"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        </TableRow>

    </TableLayout>

    <TableLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:stretchColumns="0,1,2,3"
        >
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_dark"
            android:gravity="center_horizontal">


            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:text="Name"
                android:textColor="@android:color/background_light"
                android:textAlignment="center"/>


            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:text="Qty"
                android:textColor="@android:color/background_light"
                android:textAlignment="center"/>

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:text="Price"
                android:textColor="@android:color/background_light"
                android:textAlignment="center"/>

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:text="Subtotal"
                android:textColor="@android:color/background_light"
                android:textAlignment="center"/>


        </TableRow>

    </TableLayout>


    <ScrollView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


           <!--/*Add your content here*/-->


        </LinearLayout>
    </ScrollView>
    </GridLayout>

    <GridLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:columnCount="1"
        android:rowCount="5"
        >
    <TableLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:stretchColumns="0,1,2,3"
        android:layout_gravity="bottom"
        >
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_dark"
            android:gravity="center_horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:text="Total"
                android:textColor="@android:color/background_light"
                android:textAlignment="center"/>

            <TextView
                android:id="@+id/total"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="3"
                android:textColor="@android:color/background_light"
                android:textAlignment="center"/>


        </TableRow>

        <TableRow
            android:minHeight="40dp">

            <Button
                android:id="@+id/cashOut"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_span="4"
                android:background="@android:color/holo_blue_dark"
                android:minHeight="0dp"
                android:minWidth="0dp"
                android:onClick="addItem"
                android:text="Cash Out"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        </TableRow>
    </TableLayout>

</GridLayout>

</RelativeLayout>