【问题标题】:Android customView(extends AppCompatImageView) app:srcCompat not showing imageAndroid customView(extends AppCompatImageView) app:srcCompat 不显示图像
【发布时间】:2022-01-03 10:52:55
【问题描述】:

我制作的自定义视图只是扩展了 AppCompatImageView

class BadgeImageView: AppCompatImageView {
    constructor(context: Context) : super(context) {

    }
    constructor(context: Context, attrs:AttributeSet?):super(context, attrs){

    }
    constructor(context: Context, attrs:AttributeSet?, defStyleAttr:Int):super(context, attrs,defStyleAttr){

    }
}

当我使用 AppCompatImageView 之类的..

<androidx.appcompat.widget.AppCompatImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_button_star" />

AppCompatImageView Result

当我使用 BadgeImageView 之类的..

  <com.my.proj.common.view.BadgeImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_button_star" />

BadgeImageView Result

如您所见,我只是使用了自定义视图而不是它,但它根本没有加载图像。

为什么?

【问题讨论】:

  • 你到底想要什么??图像在第一个屏幕截图中正确显示
  • @D_K 我想制作自己的自定义视图。第一个屏幕截图中的结果不是来自自定义视图,而是来自内置 AppCompatImageView。换句话说,我问为什么在我可以将其他任务应用到我的自定义视图之前,即使显示图像也会失败。

标签: android kotlin android-custom-view


【解决方案1】:

尝试使用它。我希望工作正常。

class BadgeImageView @JvmOverloads constructor(
            context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
    ) : AppCompatImageView(context, attrs, defStyleAttr) { 


    }

这里是xml代码sn-p。

<ui.components.BadgeImageView
            app:srcCompat="@drawable/ic_reported_image"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
                 app:layout_constraintBottom_toTopOf="@id/reportedTitle"
            android:layout_width="0dp"
            android:layout_height="60dp"/>

这是截图

【讨论】:

  • T3T 不工作..
  • 更新了我的答案
猜你喜欢
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多