ste*_*ven 5 android view imageview
我有一个活动,当按下按钮时调用自定义视图。自定义视图运行良好,直到我尝试向其添加 ImageView 为止。我已经在我的 xml 中尝试过此操作,该 xml 在我的主要活动中使用 setContentView 进行调用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.mypackage.mycustomview android:id="@+id/fbv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView android:id="@+id/pistolView"
android:src="@drawable/pistol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</com.mypackage.mycustomview>
Run Code Online (Sandbox Code Playgroud)
当我单击主要活动中启动此自定义视图的按钮时,我收到 ClassCastException。我想做的就是在我的自定义视图中制作一个可点击的图像。
如果我将 ImageView 放入我的主要活动和 main.xml 中,它会显示良好并单击,如下所示:
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play Vs CPU" />
<ImageView android:id="@+id/pistolView"
android:src="@drawable/pistol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)
我真的不擅长 android 中的 xml 布局,所以我不知道我在这里缺少什么。任何帮助是极大的赞赏。
这是 mycustomview 类:
package com.mypackage;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.format.Time;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class mycustomview extends View{
final Paint mPaint = new Paint();
private Context mContext;
private Resources res = getResources();
private GameControls _controls;
private GameJoystick _joystick;
private GameJoystick Rjoystick;
Paint paint = new Paint();
private long currTime;
private Time time = new Time();
private Hero mHero;
private Gun mGun;
private Bitmap heroBit;
private float possibleX;
private float possibleY;
private float lazerX;
private float lazerY;
public ArrayList<Wall> wallList = new ArrayList<Wall>();
private Canvas mCanvas;
private Bitmap splat;
private Bitmap pistolBit;
private Bitmap building;
private ImageView pistol;
private int i = 0;
private int w = 0;
Wall wall;
private RefreshHandler mRedrawHandler = new RefreshHandler();
class RefreshHandler extends Handler {
@Override
public void handleMessage(Message msg) {
//Log.d("3", "here3");
FanBoyView.this.update();
FanBoyView.this.invalidate();
}
public void sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Log.d("-------------------->code", Integer.toString(keyCode));
if (keyCode == KeyEvent.KEYCODE_FOCUS) {
//Log.d("-------------------->cam", "pressed");
fireGun();
return true;
}
return false;
}
public mycustomview(Context context, AttributeSet a) {
super(context, a);
mContext = context;
time.setToNow();
currTime = time.toMillis(false);
this.setFocusable(true);
this.setFocusableInTouchMode(true);
this.requestFocus();
final Paint paint = mPaint;
paint.setColor(0xffffffff);
paint.setAntiAlias(true);
paint.setStrokeWidth(1);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStyle(Paint.Style.STROKE);
setFocusable(true);
_joystick = new GameJoystick(mContext.getResources());
Rjoystick = new GameJoystick(mContext.getResources());
_controls = new GameControls();
setOnTouchListener(_controls);
Drawable bg = res.getDrawable(R.drawable.street);
this.setBackgroundDrawable(bg);
setWalls();
}
@Override
protected void onFinishInflate(){
//ImageView img = (ImageView) findViewById(R.id.pistolView);
/*img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("pistol","clicked");
}
});*/
}
...
Run Code Online (Sandbox Code Playgroud)
您收到 ClassCastException 因为视图不能有子视图,添加子视图适用于 ViewGroups http://developer.android.com/reference/android/view/ViewGroup.html
ImageView和TextView是普通视图,而LinearLayout和ListView是视图组。因此,后者可以有子视图,而前者则不能,这是有道理的。
您可以扩展 ViewGroup 而不是 View 并进行必要的调整,但在我看来,您应该使用相对布局,将自定义视图放在底部,将 imageview 放在顶部。
| 归档时间: |
|
| 查看次数: |
11480 次 |
| 最近记录: |