【问题标题】:Image doesn't appear when placing it in a card view将图像放置在卡片视图中时不会出现图像
【发布时间】:2019-05-03 00:17:14
【问题描述】:

我正在从 firebase 加载图像,并将它们加载到交错回收器视图中的卡片拼贴中。我希望照片的宽度保持一致并与父级匹配,并且高度根据照片的高度而变化。

我不确定我做错了什么,但图像没有显示。我可以看到照片应该在的地方有一条细线,有点像照片,但它有 1 像素的高度,只是一条水平线。

这是我的xml

<androidx.cardview.widget.CardView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:id="@+id/staggered_feed_image_card"
        app:cardCornerRadius="16dp"
        app:cardElevation="0dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp">
    <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/no_dread"
            android:id="@+id/staggered_feed_image"
            android:scaleType="centerCrop"/>
</androidx.cardview.widget.CardView>

我还尝试将 imageView 包装在另一个布局中,如下所示:

<androidx.cardview.widget.CardView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:id="@+id/staggered_feed_image_card"
        app:cardCornerRadius="16dp"
        app:cardElevation="0dp">
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/no_dread"
            android:id="@+id/staggered_feed_image"
            android:scaleType="centerCrop"/>
    </RelativeLayout>
</androidx.cardview.widget.CardView>

我尝试过相对布局或垂直线性布局,并尝试将高度设置为包装内容或匹配父级,但都不起作用。

【问题讨论】:

    标签: android android-layout layout android-cardview


    【解决方案1】:

    尝试使用下面的代码,让我知道您是否可以看到您的图片。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/viewBinary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:orientation="vertical">
    
        <androidx.cardview.widget.CardView
            android:id="@+id/staggered_feed_image_card"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            app:cardCornerRadius="16dp"
            app:cardElevation="0dp">
    
            <ImageView
                android:id="@+id/staggered_feed_image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                app:srcCompat="@drawable/splashlogo" />
        </androidx.cardview.widget.CardView>
    </LinearLayout>
    

    如果这没有帮助,请检查以下项目是否完美:

    1) implementation 'androidx.cardview:cardview:1.0.0' 确保添加了依赖项。

    2) 尝试更改drawable 并检查您的布局。

    如果您仍然遇到问题,请告诉我。

    【讨论】:

      【解决方案2】:

      使用像 RelativeLayout 或 LinearLayout 这样的容器视图作为卡片视图中的第一个子视图。然后,您可以根据需要将内容放置在该布局中。

      例子

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      xmlns:app="http://schemas.android.com/ap"
      

      然后继续上面的场景

      更新:由于 cardview 令人困惑,所以这是我主要使用的方案

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      xmlns:app="http://schemas.android.com/apk/res-auto">
      <android.support.v7.widget.CardView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="16dp">
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
              <ImageView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:scaleType="centerCrop"
                  android:src="@drawable/balon" />
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  android:padding="16dp">
                  <TextView
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginBottom="8dp"
                      android:text="@string/card_title"
                      android:textColor="#000"
                      android:textSize="18sp" />
                  <TextView
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:text="@string/card_content"
                      android:textColor="#555" />
              </LinearLayout>
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
                  <Button
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/action_share"
                      android:theme="@style/PrimaryFlatButton" />
                  <Button
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/action_explore"
                      android:theme="@style/PrimaryFlatButton" />
              </LinearLayout>
          </LinearLayout>
      </android.support.v7.widget.CardView>
      </RelativeLayout>
      

      希望对你有帮助

      【讨论】:

      • 试过但没用。我已经添加了我尝试过的内容。
      • 将此作为测试。 android:layout_height="160dp" 而不是上面的img部分
      • 当我把它交给 imageView 时它可以工作,但如果我把它交给 linear-layout/relative-layout 或 cardView 就不行
      • 我已经更新了答案希望它在你的项目中一切顺利
      • 感谢您的回答。不过,您的答案使用固定尺寸的照片,我希望每张照片都有不同的高度。它可以是横向或纵向,没有高度限制。
      【解决方案3】:

      我想你会想将scaleType 设置为fitCenteradjustViewBounds 设置为true

          <ImageView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  app:srcCompat="@drawable/no_dread"
                  android:id="@+id/staggered_feed_image"
                  android:scaleType="fitCenter"
                  android:adjustViewBounds="true"/>
      

      参考:https://proandroiddev.com/a-guide-for-android-imageview-scaletype-and-adjustviewbounds-64a1e4a35503

      【讨论】:

      • 这确实解决了根本不显示照片的问题,但现在所有照片都是1:比例。我不知道如何根据照片的实际尺寸使它们的高度动态变化。尝试了每种秤类型
      【解决方案4】:

      尝试改变

      app:srcCompat="@drawable/no_dread"
      

      android:src="@drawable/no_dread"
      

      在您的 Imageview 中,它适用于我

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-23
        • 1970-01-01
        • 2020-08-02
        • 1970-01-01
        • 1970-01-01
        • 2013-08-07
        • 2020-10-19
        • 1970-01-01
        相关资源
        最近更新 更多