位于AppBarLayout工具栏下方的视图无法点击

时间:2016-12-08 09:27:03

标签: android android-layout

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

        <include layout="@layout/custom_toolbar"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:visibility="gone"
        android:src="@android:drawable/ic_dialog_email"/>

</android.support.design.widget.CoordinatorLayout>

自定义工具栏在工具栏下方完美渲染。它包含在LinearLayout内水平堆叠的4个LinearLayouts,每个LinearLayout包含一个ImageView和一个TextView。它们代表应用程序的不同部分,我需要让它们可点击,但我不能。 Java代码是:

//MainActivity.onCreate()
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
setOnClickListenersOnToolbarItems();
...

private void setOnClickListenersOnToolbarItems(){

        View toolbarView = getLayoutInflater().inflate(R.layout.custom_toolbar, null);

        //LinearLayouts
        View clothingView = toolbarView.findViewById(R.id.clothingView3);
        View sportsView = toolbarView.findViewById(R.id.sportsView3);
        View shoesView = toolbarView.findViewById(R.id.shoesView3);
        View moreView = toolbarView.findViewById(R.id.moreView3);

        clothingView.setOnClickListener(this);
        sportsView.setOnClickListener(this);
        shoesView.setOnClickListener(this);
        moreView.setOnClickListener(this);

}

我尝试了以下事项:

  1. 这不适用,因为它错误地定位了我的观点:

    getSupportActionBar()。setCustomView() //或getCustomView()

  2. 此问题的前2个答案无效:

  3. onClick not triggered on LinearLayout with child

1 个答案:

答案 0 :(得分:2)

为您的自定义工具栏添加ID

     <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

        <include 
           android:id="@+id/mCustomToolbar"
           layout="@layout/custom_toolbar"/>

    </android.support.design.widget.AppBarLayout>

然后在java

private void setOnClickListenersOnToolbarItems(){

    View toolbarView = findViewById(R.id.mCustomToolbar);

    //LinearLayouts
    View clothingView = toolbarView.findViewById(R.id.clothingView3);
    View sportsView = toolbarView.findViewById(R.id.sportsView3);
    View shoesView = toolbarView.findViewById(R.id.shoesView3);
    View moreView = toolbarView.findViewById(R.id.moreView3);

    clothingView.setOnClickListener(this);
    sportsView.setOnClickListener(this);
    shoesView.setOnClickListener(this);
    moreView.setOnClickListener(this);

}