Android Invisible CustomView

时间:2014-04-27 09:08:28

标签: android android-imageview relativelayout android-custom-view

我创建了一个自定义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>

onMessure中的第一行有bmp,宽度和高度为234px

即使我创建一个类似setImage()的方法来从Activity调用它,图像也会被设置但仍然不可见。

1 个答案:

答案 0 :(得分:1)

你的意思是?

<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"/>