elevation=0dp 的 AppBarLayout 不响应触摸事件(点击)

AppBarLayout with elevation=0dp does not respond to touch-events (click)

我在我的应用程序中的 CoordinatorLayout 中使用了 AppBarLayout。由于某些设计要求,我被迫删除 AppBarLayout 元素下方的阴影,方法是将其高程 属性 设置为 0。(app:elevation="0")。对 AppBarLayout 中的元素执行此操作后,选项卡不响应 touch/click 事件。

通过将高度设置回 1dp,元素正在响应 touch/click 事件,但随后我又回到了阴影...

AppBarLayout 处于 0dp 高度时,有没有人建议如何让元素响应 touch/click 事件?

代码摘录:

  <android.support.design.widget.CoordinatorLayout
        android:id="@+id/rootLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/app_bar_height"
            app:elevation="0dp">

            <android.support.v7.widget.Toolbar
                android:id="@+id/my_toolbar"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:minHeight="?attr/actionBarSize">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="45dp"
                    android:scaleType="fitCenter"
                    android:layout_gravity="center"
                    android:id="@+id/toolbar_logo"
                    android:maxHeight="45dp"
                    android:contentDescription="Main logo"/>
            </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabIndicatorColor="@color/tab_indicator_color"
                app:tabTextColor="@color/primary_text_grey"
                app:tabIndicatorHeight="3dp"
                android:id="@+id/tab_layout">
            </android.support.design.widget.TabLayout>

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

通过将 CoordinatorLayout 元素替换为 LinearLayoutandroid:orientation="vertical" 来解决此问题。使用 CoordinatorLayout 似乎是一种错误的方法。

想结束这个循环,因为我 运行 遇到了一个非常相似的问题。

问题不在于 elevation=0dp,问题在于 CoordinatorLayout 的行为类似于 FrameLayout,这意味着稍后在 XML 中声明的元素是之前声明的元素的 "on top"。更改为线性布局恰好有效,因为它不支持 'overlapping elements'.

正确的解决方案是将您的 AppBarLayout(或任何元素)移动到拦截事件之后声明的任何元素之上。它在 elevation > 0 时起作用的原因是因为在调度触摸事件时考虑了 elevation,但在 elevations 相等的情况下你会遇到同样的问题。