【发布时间】: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