java-OnTouchListener 不能使用 relative layout

问题描述

OnTouchListener 不能使用 relative layout

我在一个RelativeLayout中动态的添加了很多TextViews。我现在面临的问题是:

不论什么时候当我单独申请一个 onTouch 监听器到TextViews,当我添加touch事件到relative layout时它能检测到touch 事件,但是没有回应。

检测touch事件代码:

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);

但是我在 myRelativeLayout 添加所有的 TextViews

myRelativeLayout.setOnTouchListener(cellTouch);

现在 onTouchListener 也不能调用,为什么?

<?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);,删除这条语句。

解决方案二:

在 myRelativeLayout.xml 添加:

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"

解决方案三:

加这个tv.setLongClickable(true);就可以了

时间: 2024-08-30 23:44:34

java-OnTouchListener 不能使用 relative layout的相关文章

android-表格行中的 Relative layout 布局

问题描述 表格行中的 Relative layout 布局 我在表格行中设置了相对布局,当运行下面的代码时出现 Logcat:错误,如何处理这个问题? <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" a

interface-如何把 textviews 放置在 relative layout布局的右边?

问题描述 如何把 textviews 放置在 relative layout布局的右边? 我想把两个 textviews 放置在 relative layout布局的右边,但是这两个 textviews一直在左边,怎么能放在布局的右边呢? <LinearLayout android:id="@+id/top_lay2" android:layout_width="fill_parent" android:layout_marginTop="10dp&

Android 布局(Layout)指南

Android 布局(Layout)指南 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. Android 官方文档 布局 相关资源链接汇总如下: android-sdk-macosx-4.4.2

深入Log4J源码之Layout

Layout负责将LoggingEvent中的信息格式化成一行日志信息.对不同格式的日志可能还需要提供头和尾等信息.另外有些Layout不会处理异常信息,此时ignoresThrowable()方法返回false,并且异常信息需要Appender来处理,如PatternLayout. Log4J自身实现了7个Layout类,我们可以通过继承自Layout类以实现用户自定义的日志消息格式.Log4J中已定义的Layout类结构如图: 测试类 简单的写了一个功能性测试的类,从而对不同Layout的输

Android 布局学习之——Layout(布局)详解二(常见布局和布局参数)

  [Android布局学习系列]   1.Android 布局学习之--Layout(布局)详解一   2.Android 布局学习之--Layout(布局)详解二(常见布局和布局参数)   3.Android 布局学习之--LinearLayout的layout_weight属性   4.Android 布局学习之--LinearLayout属性baselineAligned的作用及baseline      Layout Parameters(布局参数):            在XML文

服务器部署项目-linux上启动tomcat出错,java项目是编译后的,在本地测试没问题

问题描述 linux上启动tomcat出错,java项目是编译后的,在本地测试没问题 2C org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'returnedMoneyAction': Injection of resource dependencies failed; nested exception is org.springframework.beans.facto

YII 利用Clip构建layout

Yii为我们提供了layout,我们可以把一个view嵌入设计好的layout.但随着网站设计越来越多变,越来越复杂,只是layout内一个$content 变量在载入view似乎并不能满足我们的需求.如果页面中有好几个地方需要时时变动呢?没关系, Yii还为我们提供了Clip这个方法 Java代码   <?php $this->beginContent('/layouts/layout'); ?>           <?php echo $content; ?>   &l

android-把 button 放在框架布局 frame layout 左边的问题

问题描述 把 button 放在框架布局 frame layout 左边的问题 在一个垂直的线性布局中设置了两个textviews,还有一个button和一个frame layout框架布局. 我要把按钮放在框架布局的左边,并把按钮放在 relative layout布局中. 但是如何把框架布局 frame layout 放在右边呢? <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android-在 layout中不能逐个添加 imageViews

问题描述 在 layout中不能逐个添加 imageViews 我想使用一个按钮添加一个图像,然后在一个指定的relativeLayout中显示缩略图.使用下面的代码,但是不能正确运行. public void showViewOfReceiptFromSelecting(String uriString) { byte[] imageData = null; try { InputStream fis = this.getContentResolver().openInputStream(Ur