2012-06-03 131 views
0

我在检测GUI上的屏幕点击时遇到了一些麻烦。纵向工作但横向失效,请参见下文。在风景中检测点击屏幕

我有一个GUI(Fragment),其中包含一些说明+图像。用户需要点击屏幕上的任意位置才能继续。为了捕获点击/点击事件,我已经放入了填充整个屏幕并坐在其他元素上的View(顶视图),然后我听取点击这个视图并且它工作正常。

问题是在横向模式下,文本和图像占用很多空间。所以整个事情现在被包装在一个ScrollView。这是问题开始的地方。当ScrollView处于活动状态时(即您可以滚动/滚动条可见),我顶部的视图(顶视图)消失。看起来,在横向模式下,ScrollView中的内容高度正在改变。作为一个实验,我用Button取代了View,当ScrollView可用时,Button从纵向填充屏幕变为横向模式的正常高度。

有没有一种方法可以检测到用户在屏幕上点击,它与ScrollView控件一起作为顶层元素。我试过用几种方法重新排列GUI,但没有成功,我试着将onClick事件处理程序添加到ScrollView,也没有成功。

我的布局如下,注意我的顶视图是半透明的红色,所以我可以看到它覆盖的区域。

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fillViewport="true" 
android:clickable="true" > 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/txtInstructions" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal|center_vertical" 
      android:padding="10dp" 
      android:text="@string/instructions" 
      android:textColor="@color/blue" 
      android:textSize="20sp" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:adjustViewBounds="true" 
      android:maxWidth="250dp" 
      android:padding="20dp" 
      android:src="@drawable/main_camera" /> 
    </LinearLayout> 


    <View 
     android:id="@+id/view_to_listen_for_touch" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#88FF0000" 
     android:clickable="true" /> 

</RelativeLayout> 

回答

1

一两件事,工作(虽然看起来更像是一个黑客(相当丑陋的))是在代码编程添加特殊View(在onCreate方法),并设置基础上,其尺寸父母RelativeLayout的尺寸。这里是一段代码:

//... 
     final RelativeLayout parent = (RelativeLayout) findViewById(R.id.ff); 
     final View layer = new View(this); 
     layer.setBackgroundColor(Color.parseColor("#88FF0000")); 
     // the ScrollView really doesn't like this View ,using this without the 
     // runnable will not work 
     layer.setLayoutParams(new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.MATCH_PARENT, 
       RelativeLayout.LayoutParams.MATCH_PARENT)); 
     layer.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Toast.makeText(getApplicationContext(), "SDFD", 
         Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     parent.addView(layer); 
     // this is required because if we use directly the getWidth/getHeight we 
     // will get 0/0 
     layer.post(new Runnable() { 

      @Override 
      public void run() { 
       layer.setLayoutParams(new RelativeLayout.LayoutParams(parent 
         .getWidth(), parent.getHeight())); 
      } 
     }); 
//... 
+0

我不希望将scrollview作为顶层布局。我会尝试你建议的代码。我曾尝试过,但也许我做错了。当我上次尝试将视图放在滚动视图上时,在框架布局中,它停止了滚动的滚动视图。 – JonWillis

+0

刚刚尝试过你的确切代码。当视图位于滚动视图之上时,不能滚动滚动视图。它需要在里面。 – JonWillis

+0

@JonWillis抱歉,我在发布答案时完全忘记了'ScrollView'。我已经编辑了我的答案,并发布了一个解决方案,该方案应该可以在我测试它时发挥作用,但它并不那么漂亮。 – Luksprog