首页 > 解决方案 > 为什么我的桌子没有在我的卡片中滚动?

问题描述

我有一个 recyclerview 有可变数量的卡片。每张卡片代表一顿饭,并有一张桌子,里面有不同数量的食物。我试图让我的表格滚动,但我没有任何运气。

我已经尝试删除除了滚动视图之外的所有内容,为滚动视图赋予不同的权重,无论是否使用 LinearLayout 作为 TableLayout 的父级。没有任何效果。

这是我当前的布局文件:

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

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        card_view:cardBackgroundColor="@color/color_white"
        card_view:cardCornerRadius="10dp"
        card_view:cardElevation="5dp"
        card_view:cardUseCompatPadding="true">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:baselineAligned="false">

            <LinearLayout
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:layout_weight="2"
                android:orientation="vertical">

                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/fabAddFood"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:clickable="true"
                    android:focusable="true"
                    card_view:srcCompat="@android:drawable/ic_input_add" />

                <TextView
                    android:id="@+id/textViewName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="10dp"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

            </LinearLayout>

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:isScrollContainer="true"
                android:layout_weight="2">

                <TableLayout
                    android:id="@+id/foodTable"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="0dp">

                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#0079D6">

                        <TextView
                            android:id="@+id/tvFood"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:paddingLeft="@dimen/dp8"
                            android:text="@string/food"
                            android:textColor="#FFFFFF" />

                        <TextView
                            android:id="@+id/tvQty"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:paddingRight="@dimen/dp16"
                            android:text="@string/hashtag"
                            android:textColor="#FFFFFF" />
                    </TableRow>

                </TableLayout>
            </ScrollView>

        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

在卡片内滚动有什么问题吗?

谢谢您的帮助。

标签: javaandroid

解决方案


你正在使用layout_weight. 根据父视图的方向使您的子视图的高度/宽度0dp达到目的。如果您的父视图有两个具有相同权重的子视图,请注意。设置layout_weight较大的数字没有任何区别。它计算权重之间的值。

  • 水平 ->layout_width="0dp"
  • 垂直->layout_height="0dp"

     <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:baselineAligned="false">
    
            <LinearLayout
                android:layout_width="match_parent" 
                android:layout_weight="2"
                android:layout_height="0dp"  // make it 0 dp
                android:orientation="vertical"/>
    
            <ScrollView
               android:layout_width="0dp" //it should be 0dp
               android:layout_height="match_parent"
               android:isScrollContainer="true"
               android:layout_weight="2"/>
    


推荐阅读