【问题标题】:xml-layout: ScrollView cuts the bottom of an embedded CardView itemxml-layout: ScrollView 剪切嵌入 CardView 项的底部
【发布时间】:2018-05-15 13:02:55
【问题描述】:

只要 CardView 的高度足够小,不需要滚动,我在 ScrollView 中有一个可以完美运行的 CardView。但是,如果 CardView 需要滚动(以显示更大的图像),则 CardView 的底部会被剪切。我究竟做错了什么?

这是我的 layout.xml 文件的一部分:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:fillViewport="true" >

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:elevation="8dp"
    app:cardCornerRadius="8dp">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_yellow_lighten_4"
        android:nestedScrollingEnabled="false"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView_downloaded"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:scaleType="fitCenter"
            android:src="@android:drawable/ic_menu_gallery"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <!-- several other TextView elements here not shown -->

    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>

【问题讨论】:

  • 用问题分享你的整个布局
  • 我已经使用上面的代码测试了布局,省略了其他元素。结果(cardView 的底部被剪切)是一样的。
  • 您是否尝试在您的ScrollViewer 中添加android:clipToPadding="false"android:clipChildren="false"
  • 并且还使用android:layout_height="match_parent" 作为ScrollViewer
  • 设置android:layout_height="wrap_content"android:fillViewPort=true 似乎有点奇怪。你想在那里实现什么目标?

标签: android xml android-layout scrollview android-cardview


【解决方案1】:

这是我找到的解决方案:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="8dp"
    app:cardCornerRadius="8dp"
    app:cardUseCompatPadding="true"
    >
  1. 从 CardView 中删除 android:layout_margin="8dp"

  2. app:card_view:cardUseCompatPadding="true" 添加到卡片视图

【讨论】:

  • 很高兴你能修复它。这个解决方案只是让我想起了我以前遇到过但当时不记得的情况。孩子被自己的边缘剪裁很愚蠢,但不知何故似乎是合理的。
猜你喜欢
  • 1970-01-01
  • 2017-03-28
  • 2019-08-06
  • 2017-03-30
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 2020-03-26
  • 2018-04-07
相关资源
最近更新 更多