使用OnTouchListener将自定义视图添加到XML布局

时间:2011-11-06 05:16:50

标签: android xml android-layout android-xml android-view

我一直在开发一个应用程序,其中一个球(位图)出现在画布上用户点击屏幕的位置。背景是xml布局setContentView(R.layout.newsession)。画布是黑色画布。当我设置我的java父类setContentView(customView)时,该程序工作正常,但当我将自定义表面视图添加到我的XML布局和setContentView(R.layout.newsession)时,屏幕只显示画布,并且OnTouch事件没有不行。难道我做错了什么?我已经工作了近一个星期了,我真的需要帮助。我将在下面发布我的代码,用于XML布局和自定义surfaceView。提前谢谢!

XML布局(newsession)

    <FrameLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
     >

    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/newSessionPage" 
    >   

    <ImageView 
      android:layout_width="231dp" 
      android:id="@+id/ivStrikeGrid" 
      android:layout_gravity="center" 
      android:layout_height="270dp"
      android:layout_marginTop="18dp" 
      android:src="@drawable/strike_grid"
      android:layout_marginBottom="10dp"
    />

    <appsys.studios.CustomSurfaceViewOne 
     android:id="@+id/customSurfaceViewOne1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"></appsys.studios.CustomSurfaceViewOne
    >

  </FrameLayout>

自定义SurfaceView

   package appsys.studios;
   public class CustomSurfaceViewOne extends SurfaceView implements Runnable{
       public CustomSurfaceViewOne(Context context, AttributeSet attr) {
          super(context, attr);
          ourHolder = getHolder();
     }
      // Other stuff
  }

它的工作原理如下:

    newSeshView = new CustomSurfaceViewOne(this, null);
    setContentView(newSeshView);

但是当我尝试从XML布局中使用它时没有任何反应,例如:

    newSeshView = new CustomSurfaceViewOne(this, null);
    setContentView(R.layout.newsession);

再次感谢! :)

2 个答案:

答案 0 :(得分:0)

我认为您可能会错过 invallidate()在其触摸阅读代表上调用该视图。

我不确定您的代码到底发生了什么。但是,如果我要创建自己的视图并像我一样在我自己的布局中添加它。 并希望它在阅读触摸事件时改变自己,然后我会做类似的事情 在myown视图类中,它自己

   @override
    // i dont remem exact signature of this method. google it or see docs
    // motive is to read touch event of view and doing appropriate changes
    // and redrawing the view again
   public void onTouchEvent(MotionEvent me)
   {
      doCalculation(); // change points where to draw the ball next time. Read me
      invalidate();  // tell the view re draw it self

   }

希望有所帮助:)

答案 1 :(得分:0)

我遇到了同样的问题,在寻找答案时发现了你的问题。我解决了,但我不确定这是正确的方法。

3个文件,主要活动,平面视图和XML文件。

你的XML文件看起来没问题,你有表面视图

<appsys.studios.CustomSurfaceViewOne 
 android:id="@+id/customSurfaceViewOne1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"></appsys.studios.CustomSurfaceViewOne
>

在您的活动中,添加implements OnTouchListener,然后使用setContentView(R.layout.newsession);,之后仍然在onCreate()添加此行CustomViewOne cvo = (CustomViewOne)findViewById(R.id.customSurfaceViewOne1),并通过{{1}设置监听器}}

然后添加你的onTouch

cvo.setOnTouchListener(this);

其中@Override public boolean onTouch(View v, MotionEvent event) { customSurfaceViewOne1.myOnTouch(event); return false; } 是customSurfaceViewOne1类中的一个方法,您将在其中执行所有ontTouch事件。请注意,我传递了myOnTouch()

同样,我不确定这是否是正确的方法,这就是我如何让它发挥作用。我这样做的原因只是因为我可以在我的表面视图上面有一个adob广告。

相关问题