【问题标题】:View placed below toolbar in AppBarLayout not clickable在 AppBarLayout 中放置在工具栏下方的视图不可点击
【发布时间】:2016-12-08 09:27:02
【问题描述】:

这是我的布局:

<?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>

自定义工具栏在工具栏下方完美呈现。它包含 4 个在 LinearLayout 内水平堆叠的 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 个答案无效:

onClick not triggered on LinearLayout with child

【问题讨论】:

  • 查看服装View =(View)findViewById(R.id.clothingView3);
  • @Divyesh,LinearLayouts 放置在 custom_toolbar.xml 中,而不是主布局中
  • @Neonamu custom_toolbar 是一个以 LinearLayout 作为父级的常规布局。
  • 试着把这个 放在 Toolbar 标签里面,记得把 setSupportActionBar((Toolbar) findViewById(R.id.toolbar));在您的活动中,在使用它之前。
  • @Downvoter 愿意发表评论吗?

标签: android android-layout


【解决方案1】:

像这样为您的自定义工具栏提供一个 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);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    相关资源
    最近更新 更多