【问题标题】:Why does CustomView ignore drawables that are defined in app:srcCompat attribute?为什么 CustomView 会忽略 app:srcCompat 属性中定义的可绘制对象?
【发布时间】:2019-09-28 12:28:43
【问题描述】:

当通过子类化 ImageView 创建自定义视图时,自定义视图是否仍然能够呈现通过 app:srcCompat="@drawable/some_drawing" 属性定义的可绘制资源?我看不出它为什么不这样做的原因,但它对我不起作用。建筑和执行很干净,但图纸没有出现。可能是名称空间问题吗?

res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="104dp"
        tools:layout_editor_absoluteY="251dp">

        <com.example.testapp.CustomView
            android:id="@+id/customView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/some_drawing" />

java/com.example.testapp/CustomView:

class CustomView : ImageView {
    constructor(context: Context) :
            super(context) {
    }

    constructor(context: Context, attributeSet: AttributeSet) :
            super(context, attributeSet) {
    }

    constructor(context: Context, attributeSet: AttributeSet, defStyle: Int) :
            super(context, attributeSet, defStyle) {
    }
}

【问题讨论】:

  • 你试过扩展AppCompatImageView

标签: kotlin attributes android-custom-view android-drawable


【解决方案1】:

您需要扩展AppCompatImageView 而不是ImageView

【讨论】:

    猜你喜欢
    • 2014-07-10
    • 2019-10-15
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 2020-04-14
    • 2022-08-15
    • 2023-04-08
    • 2021-12-03
    相关资源
    最近更新 更多