我在包含TextView和ImageView的linearlayout上添加了一個ontouchlistener。當我觸摸它的TextView部件時,ImageView不起作用。我希望所有的linearlayout都可以獲得觸摸事件。我該怎麼做?我的XML如下:如何在LinearLayout上添加ontouchlistener或clicklistener?
<?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;
}
});
你說「我的XML是如下:」但你沒有張貼 –
不知道爲什麼它不適合您的ImageView的(需要更多的信息)工作,但在此期間,你的'onTouch()'方法可能應該返回'true'。 – Turix
是否破壞了你自己的帖子?我回滾了 –