android - 如何在 LinearLayout 上添加 ontouchlistener 或 clicklistener?

标签 android android-layout

我在 linearlayout 上添加了一个 ontouchlistener,其中包括一个 TextView 和一个 ImageView。当我触摸 TextView 部分时它起作用,而 ImageView 不起作用。我希望所有 linearlayout 都能获得触摸事件。我该怎么办?我的 XML 如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dip"
    android:background="@drawable/graytitle_bj1_black"
    android:gravity="center|bottom"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/nav_boutique"
        android:layout_width="120dip"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginBottom="2dip"
        android:clickable="true"
        android:focusableInTouchMode="true"
        android:descendantFocusability="blocksDescendants">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/description"
            android:src="@drawable/btn_jingpin_icon"
            android:background="#FFF"
            android:focusable="false"
            android:clickable="true"/>
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="#ffffff"
            android:text="@string/bottom_jingpin"/>
        <View 
            android:id="@+id/nav_boutique_line"
            android:layout_width="fill_parent"
            android:layout_height="2dip"
            android:background="#F00"/>
    </LinearLayout>
    <View 
        android:layout_width="1dip"
        android:layout_height="fill_parent"/>
    <LinearLayout
        android:id="@+id/nav_fenlei"
        android:layout_width="120dip"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginBottom="2dip"
        android:clickable="true"
        android:descendantFocusability="blocksDescendants"
        android:focusableInTouchMode="true">

        <ImageView            
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/description"
            android:src="@drawable/icon_fenlei"
            android:background="#00000000"
            android:focusable="false"
            android:clickable="true"/>
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="#ffffff"
            android:text="@string/bottom_fenlei"/>
        <View 
            android:id="@+id/nav_fenlei_line"
            android:layout_width="fill_parent"
            android:layout_height="2dip"/>
    </LinearLayout>

    <View 
        android:layout_width="1dip"
        android:layout_height="fill_parent"/>
    <LinearLayout
        android:id="@+id/nav_dingyue"
        android:layout_width="120dip"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginBottom="2dip"
        android:clickable="true"
        android:descendantFocusability="blocksDescendants"
        android:focusableInTouchMode="true">

        <ImageView            
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/description"
            android:src="@drawable/btn_zizhudingyue_icon"
            android:background="#00000000"
            android:clickable="true"
            android:focusable="false"/>
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="#ffffff"
            android:text="@string/bottom_dingyue"/>
        <View 
            android:id="@+id/nav_dingyue_line"
            android:layout_width="fill_parent"
            android:layout_height="2dip"/>
    </LinearLayout>

    <View 
        android:layout_width="1dip"
        android:layout_height="fill_parent"/>
    <LinearLayout
        android:id="@+id/nav_sousuo"
        android:layout_width="120dip"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginBottom="2dip"
        android:clickable="true"
        android:descendantFocusability="blocksDescendants"
        android:focusableInTouchMode="true">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/description"
            android:src="@drawable/icon_sousuo"
            android:background="#00000000"
            android:focusable="false"
            android:clickable="true"/>
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="#ffffff"
            android:text="@string/bottom_sousuo"/>
        <View 
            android:id="@+id/nav_sousuo_line"
            android:layout_width="fill_parent"
            android:layout_height="2dip"/>
    </LinearLayout>

</LinearLayout>

这是我的代码:

dingyueLL.setOnTouchListener(new OnTouchListener()
    {

        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            setFlagTrue(Navigation.Dingyue.getPosition());
            setBtnLine();
            return false;
        }

    });

最佳答案

使用 onintercepttouchevent 创建自定义线性布局。

public class MyLayout extends LinearLayout {
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // do whatever you want with the event
        // and return true so that children don't receive it
        return true;    
    }
}

检查这个线程。

http://developer.android.com/training/gestures/viewgroup.html

adding touch listener for liner layout filled with buttons

关于android - 如何在 LinearLayout 上添加 ontouchlistener 或 clicklistener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21225653/

相关文章:

android - 如何为android中每个不同的屏幕尺寸定义dimens.xml?

Android 为什么水平滚动不适合整个宽度

android - 如何使用 RXJava 调用的响应结果作为另一个 RXJava 函数内 if 语句的条件?

java - 在其他方法中获取 AsyncTask 的结果

java - 在启动时删除 gluon-mobile 对话框

android - Android Studio 自动调整 ConstraintLayout 大小

android - 为什么我不能为我的 RelativeLayout 设置 onTouchListener?

java - 为什么将设备语言更改为 RTL 语言时 fragment/Activity 布局看起来很糟糕?

android - 错误 :Cause: com. android.builder.BuilderConstants Android Studio

android - 如何在Android中添加自定义mime类型?