[英]CardView bottom border is cut off inside ScrollView android
我把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>
更新
現在使用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 包裝器的情況下,這對我有用。
================================================== ==========================
舊路離開這里
我遇到了同樣的問題,不得不執行以下操作(關鍵是我添加了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">
在 cardview 周圍添加LinearLayout 包裝器對我有用。
另請注意,我必須在cardview上添加card_view:cardUseCompatPadding="true"以使邊框陰影看起來正確。
這是最終結果,其中紅色框顯示了當 cardview 展開並向上滾動時添加填充的位置。
在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>
我剛剛找到的一種解決方案是用 LinearLayout 或 RelativeLayout 包裝 CardView 並設置其填充。 例如,如果你想在 cardView 中添加一些陰影效果,比如說 8dp,你可以設置你的 LinearLayout 或 RelativeLayout 的 4dp padding 和 CardView 的 4dp layout_margin。
就我而言,我只需要使用NestedScrollView
更改ScrollView
即可解決問題。
僅供參考,我的NestedScrollView
放置在一個片段中,該片段是CoordinatorLayout
的子appbar_scrolling_view_behavior
,帶有appbar_scrolling_view_behavior
集。
最好的解決方案是最后添加帶有 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
對我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.