2017-12-27 403 views
0

我正在開發購物應用程序。在一個活動中,我在表格佈局中有一個項目列表。該表具有標題行和兩個頁腳行。我只想在中間激活滾動。滾動不能僅在屏幕中間工作

爲了實現我有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> 

起初一切正常,但是當我添加了太多的項目我的滾動不起作用。包含「取出」按鈕和總文本視圖的最後兩行也會消失。我在這裏做錯了什麼?

*****************************用以下代碼解決************ *******

<?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> 

回答

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> 
+0

尾行並沒有消失,但仍然滾動不工作... – Ahnaf

+0

你能發表你的更新代碼的要點嗎? –

+0

現在它解決了..使用tablelayout而不是線性佈局...感謝引導我走向正確的方向.. – Ahnaf