OnTouchListener不与相对布局工作不与、布局、工作、OnTouchListener

2023-09-05 09:26:56 作者:挖掘机队队长

我有一个 RelativeLayout的中,我 TextViews 动态增加了许多。我现在面临的问题是,每当我申请一个onTouch监听器 TextViews 单独,它检测到接触,但是当我添加触摸到我的相对布局,它从来没有回应。

这code检测触摸事件就好了:

TextView的电视=新的TextView(本); tv.setText(值[I]); 绘制对象D = getResources()getDrawable(R.drawable.level_small_corners)。 tv.setClickable(真正的); tv.setId第(i + 1); tv.setTextSize(18); tv.setOnTouchListener(cellTouch);

但是,当我在myRelativeLayout添加所有这些TextViews:

myRelativeLayout.setOnTouchListener(cellTouch); Android XML相对布局 梅花布局

现在,该onTouchListener不会被调用。为什么会这样?

< XML版本=1.0编码=UTF-8&GT?;     < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android         机器人:layout_width =FILL_PARENT         机器人:layout_height =FILL_PARENT         机器人:背景=@机器人:彩色/黑白     >     。     。     。     <滚动型              机器人:layout_width =FILL_PARENT              机器人:layout_height =WRAP_CONTENT              机器人:layout_below =@ + ID / levelFirstLayout              机器人:layout_marginBottom =70dp              >         < RelativeLayout的              机器人:ID =@ + ID / wordsRelativeLayout              机器人:layout_width =FILL_PARENT              机器人:可点击=真              机器人:layout_height =WRAP_CONTENT 机器人:可点击=真 机器人:可聚焦=真 机器人:focusableInTouchMode =真             >         < / RelativeLayout的>         < /滚动型>     。     。     。         < / RelativeLayout的>

解决方案

  tv.setClickable(真正的);
 

导致您的布局,不要点火触摸事件。尝试删除这一点。

I have a RelativeLayout in which I am adding many TextViews dynamically. The problem I am facing is, whenever I apply an onTouch listener to TextViews individually, it detects touches but when i add touch to my relative layout, it never responds.

This code detects touch events just fine:

TextView tv = new TextView(this);
tv.setText(values[i]);
Drawable d = getResources().getDrawable(R.drawable.level_small_corners);

tv.setClickable(true);
tv.setId(i+1);
tv.setTextSize(18);
tv.setOnTouchListener(cellTouch);

But when I add all these TextViews in myRelativeLayout:

myRelativeLayout.setOnTouchListener(cellTouch);

Now, the onTouchListener never gets called. Why is that so?

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/black"
    >
    .
    .
    .

    <ScrollView 
             android:layout_width="fill_parent" 
             android:layout_height="wrap_content"
             android:layout_below="@+id/levelFirstLayout"
             android:layout_marginBottom="70dp"
             > 

        <RelativeLayout  

             android:id="@+id/wordsRelativeLayout"
             android:layout_width="fill_parent" 
             android:clickable="true"   
             android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"

            >
        </RelativeLayout>

        </ScrollView> 
    .
    .
    .
        </RelativeLayout>

解决方案

tv.setClickable(true);

is causing your layout, not to fire touch event. Try removing this.