CardView 底部边框在 ScrollView 内被切断 android
CardView bottom border is cut off inside ScrollView android
我将卡片视图放在滚动视图中,我们希望看到底部应该显示边框(见下图)。但它不是。问题是我无法滚动到底部查看 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
我刚刚找到的一个解决方案是用 LinearLayout 或 RelativeLayout 包装 CardView 并设置其填充。例如,如果你想在 cardView 中有一些投影效果,比如说 8dp,你可以设置你的 LinearLayout 或 RelativeLayout 的 4dp padding 和 CardView 的 4dp layout_margin。
已更新
现在使用 NestedScrollView
和 MaterialCardView
效果会更好。我将此 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 包装器的情况下,这对我有用。
============================================= ==============================
老办法留在这里
我 运行 遇到同样的问题,必须执行以下操作(关键是我在 cardview 周围添加了 paddingBottom 的 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 包装器 对我有用。
另请注意,我必须在 cardview 上添加 card_view:cardUseCompatPadding="true" 以使边框阴影看起来正确。
这是最终结果,其中红色框显示了扩展和向上滚动卡片视图时添加填充的位置。
最好的解决方案是最后添加带有 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>
在我的情况下,我只需将 ScrollView
更改为 NestedScrollView
即可解决问题。
仅供参考,我的 NestedScrollView
放置在一个片段中,该片段是 CoordinatorLayout
的子片段,设置了 appbar_scrolling_view_behavior
。
在 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>
我有同样的问题。将 margin 从 child 移动到 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>
我将卡片视图放在滚动视图中,我们希望看到底部应该显示边框(见下图)。但它不是。问题是我无法滚动到底部查看 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
我刚刚找到的一个解决方案是用 LinearLayout 或 RelativeLayout 包装 CardView 并设置其填充。例如,如果你想在 cardView 中有一些投影效果,比如说 8dp,你可以设置你的 LinearLayout 或 RelativeLayout 的 4dp padding 和 CardView 的 4dp layout_margin。
已更新
现在使用 NestedScrollView
和 MaterialCardView
效果会更好。我将此 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 包装器的情况下,这对我有用。
============================================= ==============================
老办法留在这里
我 运行 遇到同样的问题,必须执行以下操作(关键是我在 cardview 周围添加了 paddingBottom 的 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 包装器 对我有用。
另请注意,我必须在 cardview 上添加 card_view:cardUseCompatPadding="true" 以使边框阴影看起来正确。
这是最终结果,其中红色框显示了扩展和向上滚动卡片视图时添加填充的位置。
最好的解决方案是最后添加带有 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>
在我的情况下,我只需将 ScrollView
更改为 NestedScrollView
即可解决问题。
仅供参考,我的 NestedScrollView
放置在一个片段中,该片段是 CoordinatorLayout
的子片段,设置了 appbar_scrolling_view_behavior
。
在 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>
我有同样的问题。将 margin 从 child 移动到 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>