【问题标题】:CardView bottom border is cut off inside ScrollView androidCardView底部边框在ScrollView android内部被切断
【发布时间】:2016-07-25 16:14:06
【问题描述】:

我把cardview放在scrollview里面,我们希望看到底部应该显示边框(见下图)。但它不是。问题是我不能滚动到底部看到cardview的边框。

关于 SO 的所有解决方案都是将 layout_margins 更改为 paddings,但如果我们想显示边框,cardview 的情况并非如此。我基本上什么都试过了。但还是不行。

图片1.滚动到底部看不到边框

图2.我们可以看到上边框

下面是xml代码

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp">
                    <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:orientation="vertical"
                      >
                     ...
                    </LinearLayout>
           </CardView>
  </LinearLayout>

参考: ScrollView doesn't scroll to the bottom

ScrollView cuts off the top and leaves space at the bottom

I can't show LinearLayout at bottom to scroll view

Android ScrollView refuses to scroll to bottom

【问题讨论】:

  • 您能否附上屏幕截图以帮助我们更好地了解发生了什么?
  • @Vucko 屏幕截图已添加,谢谢

标签: android scrollview cardview


【解决方案1】:

更新

现在使用NestedScrollViewMaterialCardView 会更好。我将此NestedScrollView 添加到ConstraintLayout

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.card.MaterialCardView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:cardUseCompatPadding="true">

如果没有 LinearLayout 包装器,这对我有用。

================================================ ==============================

这里是老路

我遇到了同样的问题,必须执行以下操作(关键是我添加 paddingBottom 的 cardview 周围的 LinearLayout 包装器):

<ScrollView
    android:id="@+id/item_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    tools:visibility="visible">

    <LinearLayout
        android:id="@+id/item_wrapper_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/content_margin"
        android:paddingBottom="32dp"
        android:orientation="vertical">

        <android.support.v7.widget.CardView
            android:id="@+id/item_cardview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardBackgroundColor="@color/colorPrimary"
            card_view:cardCornerRadius="4dp"
            card_view:cardUseCompatPadding="true">

在卡片视图周围添加 LinearLayout 包装器对我有用。

另外请注意,我必须在卡片视图上添加 card_view:cardUseCompatPadding="true" 才能使边框阴影看起来正确。

这是最终结果,当卡片视图展开并向上滚动时,红色框显示已添加填充的位置。

【讨论】:

  • 我不太喜欢添加一个额外的层来包裹卡片视图,遍历视图树需要额外的时间,尤其是当视图很深时更是如此。如果您不关心性能,那应该可以
  • 添加一个 LinearLayout 作为包装器不会导致这个设计的性能大幅提升。如果这都包含在 RecyclerView 中,那么我可以看到这可能是一个问题。无论如何,这样的视图不应该那么深——尤其是在卡片上方。
  • 如果您看到上面的屏幕截图,显然存在边距或填充问题。滚动条即使到达底部也无法再滚动。所以修复这个填充/边距问题是正确的方法。包装线性布局只是一种黑客方式,或者恕我直言,它的工作原理是巧合。
  • 我无法完成这项工作,唯一有效的是在CardView 中添加app:cardUseCompatPadding="true",如this answer中所述。谢谢!
【解决方案2】:

ScrollView 上将clipToPadding 设置为false 通常对我有用:

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    android:paddingBottom="16dp">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        app:contentPadding="8dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="Lorem ipsum..." />

    </com.google.android.material.card.MaterialCardView>

</ScrollView>

【讨论】:

  • 只需将 clipToPadding 添加到 false 到 MaterialCardView 的父级对我有用
  • 这需要clipToPadding="false"paddingBottom。如果只添加clipToPadding,将无法正常工作。
【解决方案3】:

我刚刚找到的一个解决方案是使用 LinearLayout 或 RelativeLayout 包装 CardView 并设置其填充。例如,如果您想在 cardView 中添加阴影效果,比如说 8dp,您可以设置 LinearLayout 或 RelativeLayout 的 4dp padding 和 CardView 的 4dp layout_margin。

【讨论】:

  • 是的,但是添加额外的层会花费额外的时间来遍历未优化的视图树。好吧,如果您不关心性能,那应该可以
  • 我什至不必设置填充。刚刚添加了线性布局,就解决了问题
【解决方案4】:

就我而言,我只需将ScrollView 更改为NestedScrollView 即可解决问题。

仅供参考,我的NestedScrollView 被放置在一个片段中,该片段是CoordinatorLayout 的子片段,带有appbar_scrolling_view_behavior 集。

【讨论】:

    【解决方案5】:

    最好的解决方案是最后添加带有marginTop的View

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"> 
    
            <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
    
    
          ...................
          ...................
    
           <View
             android:layout_width="match_parent"
             android:layout_height="0.1dp"
             android:layout_marginTop="10dp"/>
    
       </LinearLayout>
    
    </ScrollView>
    

    【讨论】:

      【解决方案6】:

      我有同样的问题。将边距从孩子移动到 ScrollView 对我有用

      <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:custom="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      >
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:fillViewport="true"
              android:layout_margin="8dp">
      
                  <android.support.v7.widget.CardView
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content">
                          <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical"
                            >
                           ...
                          </LinearLayout>
                 </CardView>
        </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-02
        • 1970-01-01
        • 2014-09-23
        • 1970-01-01
        • 2016-02-03
        • 2016-11-03
        相关资源
        最近更新 更多