Android不可见自定义视图

Android不可见自定义视图,android,android-imageview,android-relativelayout,android-custom-view,Android,Android Imageview,Android Relativelayout,Android Custom View,我已经创建了一个自定义的RelativeLayout,其中有两个ImageView,一个在另一个之上,第一个应该显示用户选择的图像,第二个在用户选择后显示并充当删除按钮(小X图标),我遇到的问题是,即使在应用程序开始时,这个自定义视图是不可见的,但我仍然可以单击它。如果我用背景,我就能看到它。 这是我的自定义视图类和XML public class ImageViewWithIcon extends RelativeLayout{ public ImageView image

我已经创建了一个自定义的
RelativeLayout
,其中有两个
ImageView
,一个在另一个之上,第一个应该显示用户选择的图像,第二个在用户选择后显示并充当删除按钮(小X图标),我遇到的问题是,即使在应用程序开始时,这个自定义视图是不可见的,但我仍然可以单击它。如果我用背景,我就能看到它。 这是我的自定义视图类和XML

 public class ImageViewWithIcon extends RelativeLayout{
    public ImageView      image, icon;
    private final Context context;

    public ImageViewWithIcon(final Context context)
    {
        super(context);
        this.context = context;
    }

    public ImageViewWithIcon(final Context context, final AttributeSet attrs)
    {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.image_view_with_icon, this, true);
        RelativeLayout rl = (RelativeLayout) getChildAt(0);
        icon = (ImageView) findViewById(R.id.ImageViewWithIcon_icon);
        image = (ImageView) findViewById(R.id.ImageViewWithIcon_image);
        // image.setLayoutParams(new LayoutParams(400, 400));
        this.context = context;
    }

    public ImageViewWithIcon(final Context context, final AttributeSet attrs, final int defStyle)
    {
        super(context, attrs, defStyle);
        this.context = context;
    }

    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec)
    {
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.new_message_picture_button);
        int width;
        if (getLayoutParams().width == android.view.ViewGroup.LayoutParams.WRAP_CONTENT)
            width = bmp.getWidth();
        else
            if (getLayoutParams().width == android.view.ViewGroup.LayoutParams.MATCH_PARENT || getLayoutParams().width == android.view.ViewGroup.LayoutParams.FILL_PARENT)
                width = MeasureSpec.getSize(widthMeasureSpec);
            else
                width = getLayoutParams().width;
        int height = 100;
        if (getLayoutParams().height == android.view.ViewGroup.LayoutParams.WRAP_CONTENT)
            height = bmp.getHeight();
        else
            if (getLayoutParams().height == android.view.ViewGroup.LayoutParams.MATCH_PARENT || getLayoutParams().height == android.view.ViewGroup.LayoutParams.FILL_PARENT)
                height = MeasureSpec.getSize(heightMeasureSpec);
            else
                height = getLayoutParams().height;
        bmp.recycle();
        setMeasuredDimension(width | MeasureSpec.EXACTLY, height | MeasureSpec.EXACTLY);
    }}


<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/ImageViewWithIcon_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:background="@drawable/new_message_picture_button" />

        <ImageView
            android:id="@+id/ImageViewWithIcon_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_alignBottom="@id/ImageViewWithIcon_image"
            android:visibility="visible"
            android:src="@drawable/new_message_close_picture"/>
    </RelativeLayout>
</merge>
带有图标的公共类ImageView扩展了RelativeLayout{
公共图像查看图像,图标;
私人最终语境;
带有图标的公共图像视图(最终上下文)
{
超级(上下文);
this.context=上下文;
}
带有图标的公共图像视图(最终上下文、最终属性集属性)
{
超级(上下文,attrs);
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
充气器。充气(R.layout.image\u view\u,带有图标,此为真);
RelativeLayout rl=(RelativeLayout)getChildAt(0);
icon=(ImageView)findViewById(R.id.ImageView带有icon\u图标);
image=(ImageView)findViewById(R.id.ImageViewWithIcon\u image);
//setLayoutParams(新的LayoutParams(400400));
this.context=上下文;
}
带有图标的公共图像视图(最终上下文上下文、最终属性集属性、最终整型定义样式)
{
超级(上下文、属性、定义样式);
this.context=上下文;
}
@凌驾
测量时的保护空隙(最终内部宽度测量等级、最终内部高度测量等级)
{
位图bmp=BitmapFactory.decodeResource(getResources(),R.drawable.new\u message\u picture\u按钮);
整数宽度;
if(getLayoutParams().width==android.view.ViewGroup.LayoutParams.WRAP\u内容)
宽度=bmp.getWidth();
其他的
如果(getLayoutParams().width==android.view.ViewGroup.LayoutParams.MATCH_PARENT | | getLayoutParams().width==android.view.ViewGroup.LayoutParams.FILL_PARENT)
宽度=MeasureSpec.getSize(widthMeasureSpec);
其他的
宽度=getLayoutParams().width;
整数高度=100;
if(getLayoutParams().height==android.view.ViewGroup.LayoutParams.WRAP_CONTENT)
height=bmp.getHeight();
其他的
如果(getLayoutParams().height==android.view.ViewGroup.LayoutParams.MATCH_PARENT | | getLayoutParams().height==android.view.ViewGroup.LayoutParams.FILL_PARENT)
高度=测量等级getSize(高度测量等级);
其他的
高度=getLayoutParams()。高度;
bmp.recycle();
设置测量尺寸(宽度|精确测量,高度|精确测量);
}}
压力
中的第一行有宽度和高度为234px的bmp

即使我创建了一个像
setImage()
这样的方法从
活动调用它,图像也会被设置,但它仍然是不可见的。

你的意思是

<RelativeLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageViewWithIcon
                android:id="@+id/ImageViewWithIcon_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                android:background="@drawable/new_message_picture_button" />

            <ImageViewWithIcon
                android:id="@+id/ImageViewWithIcon_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:layout_alignBottom="@id/ImageViewWithIcon_image"
                android:visibility="visible"
                android:src="@drawable/new_message_close_picture"/>


不,我发布的XML是带有图标XML的ImageView,其中有两个常规ImageView。在onMeasure方法中,widthMeasureSpec和heightMeasureSpec可能是0。因为您的RelativeLayout具有wrap_内容,但在调用onMeasure时它是have not content。widthMeasureSpec和heightMeasureSpec的大小与位图大小相同,在本例中为234 px,如果我在mainActivity.XML中使用该视图的background属性,我可以看到它。