如何扩展ImageView

时间:2017-01-16 11:08:10

标签: android android-layout imageview

我需要创建自己的 ImageView 。 这是我的班级:

public class Brick extends ImageView implements Serializable{

    public Brick(Context context) {
        super(context);
    }

    public Brick(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Brick(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

}

当我尝试在xml 布局文件中使用 ImageView 时(如下所示)我遇到了问题。我可以看到一个黑色的形状,但里面没有图像(可绘制的d)。

 <com.myapp.Brick
        android:id="@+id/myBrick"
        app:srcCompat="@drawable/d"
        android:background="#000000"
        android:layout_width="300dp"
        android:layout_height="300dp" />

我的错误是什么?

2 个答案:

答案 0 :(得分:3)

您应该在 XML 部分中调用android:src="@drawable

<com.myapp.Brick
    android:id="@+id/myBrick"
    app:srcCompat="@drawable/d"
    android:background="#000000" // showing Black Shape Background
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:src="@drawable/add_image /> // Add  android:src

答案 1 :(得分:1)

您应该在 XML 部分

中添加app:srcCompatandroid:src