簡體   English   中英

CardView底部邊框在ScrollView android中被切斷

[英]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>

參考: ScrollView 不會滾動到底部

ScrollView 切斷頂部並在底部留出空間

我無法在底部顯示 LinearLayout 以滾動視圖

Android ScrollView 拒絕滾動到底部

更新

現在使用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">

在 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM