Android customView(扩展AppCompatImageView)应用程序:srcCompat不显示图像

问题描述 投票:0回答:1

我制作的自定义视图只是扩展了 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

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

为什么?

android kotlin android-custom-view
1个回答
-1
投票

尝试使用这个。希望工作顺利。

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


    }

此处为 Xml 代码片段。

<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"/>

这是截图

© www.soinside.com 2019 - 2024. All rights reserved.