【问题标题】:Android Invisible CustomViewAndroid 不可见的自定义视图
【发布时间】:2014-04-27 09:08:28
【问题描述】:

我创建了一个自定义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 调用它,图像已设置但它仍然不可见。

【问题讨论】:

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


    【解决方案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"/>
    

    【讨论】:

    • 不,我发布的 XML 是 ImageViewWithIcon XML,里面有两个常规的 ImageView。
    • 可能在您的onMeasure方法中-widthMeasureSpec和heightMeasureSpec为0。因为您的RelativeLayout有wrap_content,但在调用onMeasure时它没有内容。
    • widthMeasureSpec 和 heightMeasureSpec 与位图大小相同,在本例中为 234 px,如果我在 MainActiviry.XML 中使用该视图的背景属性,我可以看到它。跨度>
    猜你喜欢
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多