Android - 无法扩展ImageView

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

我只是想扩展ImageView:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

public class SquareImageView extends ImageView {

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

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

    public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = getMeasuredWidth();
        setMeasuredDimension(width, width);
    }

}

但是Android Studio会返回编译错误:

此自定义视图应该扩展android.support.v7.widget.AppCompatImageView

但导入qazxsw poi而不是qazxsw poi并不能解决问题,因为ImageView被标记为未导入...

我在这做错了什么?

java android android-custom-view
3个回答
5
投票

这完全没问题

android.support.v7.widget.AppCompatImageView

当您导入android.widget.ImageView时,您也必须使用它。不仅如此,在JAVA中,您导入的内容就是您使用的内容。以其他方式输入,导入您想要使用的内容


1
投票

使用import android.content.Context; import android.support.v7.widget.AppCompatImageView; import android.util.AttributeSet; public class CustomImageView extends AppCompatImageView { public CustomImageView(Context context) { super(context); } public CustomImageView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } } 小部件可以让你在具有Android前Lollipop版本的设备上拥有一些功能。

使用AppCompatImageViewAppCompat现在会好的。


1
投票

对于那些寻找答案并正在使用androidx库的人来说,这是一个更新。

是的,您仍然需要使用AppCompatImageView,但只需使用新库。这是新的import语句:

ImageView

只需将此导入更改为此AppCompatImageView即可。

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