Android 未单击工具栏内部的视图

Android 未单击工具栏内部的视图,android,android-toolbar,Android,Android Toolbar,在我的应用程序中,我使用的是工具栏。现在在我的工具栏中有两个图像视图。当用户单击任何图像视图时,我会打开不同的片段或活动,但在我的情况下,我无法单击任何图像视图 *.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

在我的应用程序中,我使用的是
工具栏
。现在在我的工具栏中有两个
图像视图
。当用户单击任何图像视图时,我会打开不同的
片段
活动
,但在我的情况下,我无法单击任何图像视图

*.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="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_home"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@drawable/background"
        android:elevation="2dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/iv_home_about_us"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:src="@drawable/about" />

            <ImageView
                android:id="@+id/iv_home_search"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignTop="@+id/iv_home_about_us"
                android:layout_marginRight="10dp"
                android:src="@drawable/search" />
        </RelativeLayout>

    </android.support.v7.widget.Toolbar>

    <TextView
        style="@style/text_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="05dp"
        android:gravity="center"
        android:text="Trending around me" />

    <com.techmorphosis.wheretoday.SlidingTabs.SlidingTabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:padding="10dp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"></android.support.v4.view.ViewPager>

</LinearLayout>

您无法在
onCreateView
method-EVER中访问UI元素

使用
onActivityCreated
方法,告诉片段活动已完全创建并准备好交互

@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    ImageView ivAboutUs = (ImageView) view.findViewById(R.id.iv_home_about_us);
    ivAboutUs .setOnClickListener(new OnClickListener() {

       @Override
        public void onClick(View v) {
                            }
                    });
}

您无法在
onCreateView
method-EVER中访问UI元素

使用
onActivityCreated
方法,告诉片段活动已完全创建并准备好交互

@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    ImageView ivAboutUs = (ImageView) view.findViewById(R.id.iv_home_about_us);
    ivAboutUs .setOnClickListener(new OnClickListener() {

       @Override
        public void onClick(View v) {
                            }
                    });
}

这可能是一个触摸事件永远不会到达ImageView的问题,因为它们被其他东西吸收了。我遇到了类似的情况,我试图将onClickListener放在嵌入列表项的Imageview上。无论我在imageView上点击了多少次,都有其他东西在吞噬触摸事件(很可能是组成ListView项目的视图)。我通过将imageView更改为ImageButton并将其背景设置为null(这样ImageButton看起来像imageView而不是图像按钮)修复了该问题完成此操作后,单击图像对我很有效。请尝试将您的ImageView更改为如下所示:

<ImageButton
            android:id="@+id/iv_home_about_us"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:background="@null"
            android:src="@drawable/about" />

        <ImageView
            android:id="@+id/iv_home_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/iv_home_about_us"
            android:layout_marginRight="10dp"
            android:background="@null"
            android:src="@drawable/search" />


我希望这会有所帮助。祝你好运。

这可能是一个触摸事件无法到达Imageview的问题,因为它们被其他东西吸收了。我遇到了类似的情况,我试图在嵌入列表项的Imageview上放置onClickListener。无论我在Imageview上单击了多少次,其他东西都在吞噬触摸事件(最有可能是组成ListView项的视图。我通过将ImageView更改为ImageButton并将其背景设置为null(使ImageButton看起来像ImageView而不是图像按钮)修复了该问题完成此操作后,单击图像对我很有效。请尝试将您的ImageView更改为如下所示:

<ImageButton
            android:id="@+id/iv_home_about_us"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:background="@null"
            android:src="@drawable/about" />

        <ImageView
            android:id="@+id/iv_home_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/iv_home_about_us"
            android:layout_marginRight="10dp"
            android:background="@null"
            android:src="@drawable/search" />


我希望这有帮助。祝你好运。

我认为片段
ivAboutUs.setOnClickListener(这个);
不起作用,不知道为什么。试试
ivAboutUs.setOnClickListener(新OnClickListener(){@Override public void onClick(视图v){//TODO自动生成的方法存根})
我认为片段内部
ivAboutUs.setOnClickListener(这个);
不起作用不知道为什么。试试
ivAboutUs.setOnClickListener(新的OnClickListener(){@Override public void onClick(View v){//TODO自动生成的方法stub}});
我认为片段内部
ivAboutUs.setOnClickListener(这);
不起作用不知道为什么。请尝试
ivAboutUs.setOnClickListener(新OnClickListener(){@Override public void onClick(视图v){//TODO Auto-generated method stub}});
Sir它在onActivityCreated()中您是否尝试为
视图
而不是
工具栏
查找ImageView?是否尝试为
视图
而不是
工具栏查找ImageView?是否尝试为
视图
而不是
工具栏查找ImageView?是否尝试为
工具栏
查找ImageView?