【问题标题】:Problem with layout constructing in AndroidAndroid中布局构建的问题
【发布时间】:2021-05-12 14:29:49
【问题描述】:

我的 recyclerview 项目有一个布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="8dp"
    android:orientation="vertical">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/iv_hotel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_launcher_background" />

        <TextView
            android:id="@+id/tv_hotel_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/iv_hotel"
            tools:text="Belleclaire Hotel" />

        <include
            android:id="@+id/include"
            layout="@layout/include_stars_set"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="@+id/tv_hotel_name"
            app:layout_constraintTop_toBottomOf="@+id/tv_hotel_name" />

        <TextView
            android:id="@+id/tv_hotel_address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="@+id/include"
            app:layout_constraintTop_toBottomOf="@+id/include"
            tools:text="250 West 77th Street, Manhattan" />

        <TextView
            android:id="@+id/tv_distance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="@+id/tv_hotel_address"
            app:layout_constraintTop_toBottomOf="@+id/tv_hotel_address"
            tools:text="100.0" />

        <TextView
            android:id="@+id/tv_suites_available_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/label_suites_availability"
            app:layout_constraintStart_toStartOf="@+id/tv_distance"
            app:layout_constraintTop_toBottomOf="@+id/tv_distance" />

        <TextView
            android:id="@+id/tv_suites_available"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintStart_toEndOf="@+id/tv_suites_available_label"
            app:layout_constraintTop_toTopOf="@+id/tv_suites_available_label" />

        <ProgressBar
            android:id="@+id/progress_hotel_image"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="@+id/iv_hotel"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/iv_hotel" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

好像是这样的:

enter image description here

但是在构建之后我有这个视图:

enter image description here

如您所见,在 Glide 的帮助下尝试将图像下载到 iv_hotel 后,我只看到一半的进度条。 我怎么解决这个问题?我对xml指南的理解有点差,所以我寻求帮助(

【问题讨论】:

    标签: android android-layout android-xml android-constraintlayout


    【解决方案1】:

    在请求进行时使用占位符显示默认图像

    Glide.with(context)
      .load(url)
      .placeholder(R.drawable.placeholder)
      .into(view);
    

    【讨论】:

      【解决方案2】:
       if (!imageUrl.isNullOrEmpty()) {
                  imageView?.let { iv ->
                      Glide.with(iv.context)
                              .load(AppUtility.IMAGE_THUMB_URL.plus(imageUrl))
                              .apply(options)
                              .into(iv)
                  }
              } else {
                  imageView?.setImageDrawable(error)
               // set here your error img
              }
      

      【讨论】:

        猜你喜欢
        • 2011-06-09
        • 1970-01-01
        • 2019-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多