2010-09-15 78 views
4

我有一個活動,當按下按鈕時調用自定義視圖。自定義視圖運行良好,直到我嘗試添加一個ImageView。我在我的XML被稱爲在我的主要活動與的setContentView嘗試這樣:如何在Android中的自定義視圖中顯示ImageView

<?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> 

我得到一個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"/> 

我真的不擅長用XML佈局在android中,所以我不知道我在這裏失蹤。任何幫助是極大的讚賞。

這裏是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"); 
         } 
        });*/ 
      } 
... 
+1

你可以粘貼mycustomview類? – Cristian 2010-09-15 04:05:53

+0

對不起,這是我第一次在這裏發帖,我無法找到編輯的地方......這裏是課程的頂部,我已經改變了課程名稱,並刪除了一些隱藏遊戲本質的代碼,但我不想在當前代碼中的任何地方引用Imageview,你可以看到我已經在onFinishInflate()中引用它的地方的註釋: – steven 2010-09-15 20:12:35

+0

bleh,我無法在評論中適合該類... I我想知道如何編輯我的帖子 – steven 2010-09-15 20:13:22

回答

0

你得到一個ClassCastException因爲視圖不能有子視圖,增加孩子的觀點是ViewGroups http://developer.android.com/reference/android/view/ViewGroup.html

的ImageView和TextView的是正常的意見,而LinearLayout和ListView是視圖組。因此,後者可以有孩子的觀點,而前者不能。

您可以擴展ViewGroup而不是View並進行必要的調整,但聽起來像您應該使用相對佈局,將自定義視圖放在底部,並將imageview放在最上面。

0

首先,Lee Chou說你需要讓你的課程從視圖組延伸到相對佈局,線性佈局等。

public class CustomView extends RelativeLayout { 
Context context; 
private ImageView imgView; 
private TextView lblView; 
LayoutInflater inflater; 
/*Do I need all three constructors for an Android custom view?*/ 

//if you add your View from xml and also spcify the android:style attribute like : <com.mypack.MyView style="@styles/MyCustomStyle" /> 
//you will also need the first constructor public MyView(Context context, AttributeSet attrs,int defStyle) 
public CustomView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    this.context = context; 
    init(); 
} 
//you will need the constructor public MyView(Context context, AttributeSet attrs), otherwise you will get an Exception when Android tries to inflate your View. 
public CustomView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.context = context; 
    init(); 
} 

//The third constructor is usually used when you extend a style and customize it, and then you would like to set that style to a given View in your layouts 
public CustomView(Context context) { 
    super(context); 
    this.context = context; 
    init(); 
} 



public void init() 
{ 
    LayoutInflater.from(context).inflate(R.layout.widget_customview_main, this); 
    lblView =(TextView) findViewById(R.id.lblView); 
    imgView = (ImageView) findViewById(R.id.imgView); 
} 

這裏是XML R.layout.widget_customview_main

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
class = "com.ikolmobile.satso." 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TextView 
    android:id="@+id/lblView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="60dp" 
    android:text="My Custom View" /> 

<ImageView 
    android:id="@+id/imgView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="31dp" 
    android:src="@drawable/abc_ab_bottom_solid_dark_holo" /> 

</RelativeLayout>