0
我已经创建了一个自定义RelativeLayout
,其中有两个ImageView
,一个在另一个之上,第一个应该显示用户选择的图像,第二个显示出来后他选择并行动作为一个删除按钮(小X图标),我的问题是即使在应用程序的开始这个自定义视图是不可见的,但我仍然可以点击它。如果我使用它的背景,我可以看到它。 这里是我的自定义视图类和XMLAndroid Invisible CustomView
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
调用,图像已设置,但仍然不可见。
不,我张贴的XML是ImageViewWithIcon XML,它里面有拖定期ImageViews。 –
可能在您的onMeasure方法中 - widthMeasureSpec和heightMeasureSpec为0.因为您的RelativeLayout具有wrap_content,但在调用onMeasure时它没有内容。 –
widthMeasureSpec和heightMeasureSpec与位图大小相同,在本例中为234 px,如果我在MainActiviry.XML中使用该视图的背景属性,则可以看到它。 –