Android 如何在LinearLayout上添加ontouchlistener或clicklistener?
我在linearlayout上添加了一个ontouchlistener,其中包括一个TextView和一个ImageView。当我触摸TextView部件时,它工作,而ImageView不工作。我希望所有的linearlayout都可以获得触摸事件。我该怎么办?我的XML如下所示:Android 如何在LinearLayout上添加ontouchlistener或clicklistener?,android,android-layout,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/r
<?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>
我要尝试的是在图像中显式地添加一个触控监听器,看看会发生什么。在这个侦听器中,不要调用晚餐,并返回false,这样事件将传播到下一个侦听器,希望是布局
dingyueLL.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
setFlagTrue(Navigation.Dingyue.getPosition());
setBtnLine();
return false;
}
});
THEIMAGE.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
return false;
}
});
另外,在图像监听器的ontouch函数中放置一个断点,以查看它是否正在到达那里我将尝试显式地向图像添加一个触摸监听器,并查看发生了什么。在这个侦听器中,不要调用晚餐,并返回false,这样事件将传播到下一个侦听器,希望是布局
dingyueLL.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
setFlagTrue(Navigation.Dingyue.getPosition());
setBtnLine();
return false;
}
});
THEIMAGE.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
return false;
}
});
另外,在图像侦听器的ontouch函数中放置一个断点,以查看它是否正在到达该位置使用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;
}
}
检查这条线
使用
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;
}
}
检查这条线
您说“我的XML如下:”但您没有发布它不确定它为什么不适用于您的ImageView(需要更多信息),但与此同时,您的
onTouch()
方法可能会返回true
。您是否破坏了自己的帖子?我将其回滚您已使ImageView
可点击,这就是为什么它正在消费触摸事件并阻止它们返回到父级LinearLayout
。很抱歉,这是我第一次使用它,它让我发疯。您说“我的XML如下:”但您没有发布它不确定它为什么不适用于您的ImageView(需要更多信息),但与此同时,您的onTouch()
方法可能会返回true
。你破坏了你自己的帖子吗?我把它回滚了你让图像视图
可点击,这就是为什么它会消耗触摸事件并阻止它们返回到父级线性布局
。很抱歉,这是我第一次使用它,它让我发疯了谢谢你。这真的很有帮助。你说得太对了!事实上我几天前看到这个,我不记得了,但这是一个优雅的解决方案!+1谢谢你。这真的很有帮助。你说得太对了!事实上我几天前看到这个,我不记得了,但这是一个优雅的解决方案!+1