[英]CardView bottom border is cut off inside ScrollView android
I am putting the cardview inside scrollview, we expect to see that at the bottom, the border should be shown(see pic below).我把cardview放在scrollview里面,我们希望看到在底部,应该显示边框(见下图)。 But its not.但它不是。 The problem is that I cannot scroll to the bottom to see the border of cardview.问题是我无法滚动到底部以查看 cardview 的边框。
All the solutions on SO is to change layout_margins to paddings, but its not the case for cardview if we want to show the border. SO上的所有解决方案都是将layout_margins更改为paddings,但如果我们想显示边框,则不是cardview的情况。 I basically tried everything.我基本上什么都试过了。 But still doesnt work.但仍然不起作用。
Picture 1. scroll to bottom cannot see the border图1.滚动到底部看不到边框
Picture 2. We can see the top border图 2. 我们可以看到顶部边框
Following is xml code以下是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>
references: ScrollView doesn't scroll to the bottom参考: ScrollView 不会滚动到底部
ScrollView cuts off the top and leaves space at the bottom ScrollView 切断顶部并在底部留出空间
I can't show LinearLayout at bottom to scroll view 我无法在底部显示 LinearLayout 以滚动视图
Android ScrollView refuses to scroll to bottom Android ScrollView 拒绝滚动到底部
UPDATED更新
This will work better now with a NestedScrollView
and a MaterialCardView
.现在使用NestedScrollView
和MaterialCardView
效果会更好。 I added this NestedScrollView
to a ConstraintLayout
.我将此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">
This is working for me without the LinearLayout wrapper.在没有 LinearLayout 包装器的情况下,这对我有用。
=========================================================================== ================================================== ==========================
OLD WAY LEFT HERE旧路离开这里
I ran into the same problem and had to do the following (the key is the LinearLayout wrapper around the cardview where I added the paddingBottom ):我遇到了同样的问题,不得不执行以下操作(关键是我添加了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">
Adding the LinearLayout wrapper around the cardview is what worked for me.在 cardview 周围添加LinearLayout 包装器对我有用。
Also note, I had to add card_view:cardUseCompatPadding="true" on the cardview to get the border shadow looking correct.另请注意,我必须在cardview上添加card_view:cardUseCompatPadding="true"以使边框阴影看起来正确。
Here is the end result where the red box shows where the padding has been added when the cardview is expanded and scrolled up.这是最终结果,其中红色框显示了当 cardview 展开并向上滚动时添加填充的位置。
Setting clipToPadding
to false
on a ScrollView
usually works for me:在ScrollView
clipToPadding
设置为false
通常对我clipToPadding
:
<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>
One solution I just found is to wrap CardView with a LinearLayout or RelativeLayout and set its padding.我刚刚找到的一种解决方案是用 LinearLayout 或 RelativeLayout 包装 CardView 并设置其填充。 For example, If you want some drop shadow effect in cardView, lets say 8dp, you can set 4dp padding of your LinearLayout or RelativeLayout and 4dp layout_margin of CardView.例如,如果你想在 cardView 中添加一些阴影效果,比如说 8dp,你可以设置你的 LinearLayout 或 RelativeLayout 的 4dp padding 和 CardView 的 4dp layout_margin。
In my case I just have to change ScrollView
with NestedScrollView
to solve the problem.就我而言,我只需要使用NestedScrollView
更改ScrollView
即可解决问题。
FYI, my NestedScrollView
is placed inside a fragment which is a child of CoordinatorLayout
with appbar_scrolling_view_behavior
set.仅供参考,我的NestedScrollView
放置在一个片段中,该片段是CoordinatorLayout
的子appbar_scrolling_view_behavior
,带有appbar_scrolling_view_behavior
集。
The best solution is adding View with marginTop at last最好的解决方案是最后添加带有 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>
I had same issue.我有同样的问题。 Moving margin from child to ScrollView
worked for me将边距从孩子移动到ScrollView
对我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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.