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中使用該視圖的背景屬性,則可以看到它。 –