【发布时间】:2016-12-28 09:58:28
【问题描述】:
CollapsingToolbarLayout 中有三个按钮。展开后,想法是修改正在显示的图像库上的过滤器,或弹出一个编辑对话框。我得到的结果不一致——按钮只是间歇性地响应点击。
最终,我意识到问题在于可点击区域远小于视图的客户端矩形。在水平方向上,它们看起来很正常,但在垂直方向上,可点击区域比按钮短得多。在模拟器中,我能够获得相当精确的边界:
你可以正常从左到右触摸它们,但从上到下是错误的。
我一直在尝试从文档、官方指南、在线教程和开源代码示例中的各种 sn-ps 拼凑出这个布局。我不完全理解所有花哨的支持/设计布局是如何一起工作的,或者所有配置属性在组合时究竟做了什么,所以很可能修复将是一个或两个属性的简单更改。这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activity.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="?android:actionBarSize"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
app:contentScrim="?attr/colorPrimary"
android:background="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
android:theme="@style/Widget.Design.CollapsingToolbar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="32dp"
android:paddingBottom="64dp"
android:fitsSystemWindows="true"
app:layout_collapseMode="none"
android:background="@android:color/transparent"
android:orientation="horizontal">
<ImageButton android:id="@+id/btnTags"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_weight="0.3"
android:src="@drawable/ic_tag"
android:tint="?android:attr/buttonTint"
android:background="@drawable/ripple" />
<ImageButton android:id="@+id/btnAlbums"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_weight="0.3"
android:src="@drawable/ic_albums"
android:tint="?android:attr/buttonTint"
android:background="@drawable/ripple" />
<ImageButton android:id="@+id/btnNewAlbum"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_weight="0.3"
android:src="@drawable/ic_new_album"
android:tint="?android:attr/buttonTint"
android:background="@drawable/ripple" />
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarMain"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:title="@string/app_name"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/LouvreTheme.ToolbarStyle"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<co.moonmonkeylabs.realmrecyclerview.RealmRecyclerView
android:id="@+id/recyclerAlbumGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="?android:attr/background"
app:rrvLayoutType="Grid"
app:rrvGridLayoutSpanCount="@integer/grid_span"
app:rrvIsRefreshable="false"
app:rrvSwipeToDelete="false" />
</android.support.design.widget.CoordinatorLayout>
然后,在我的onViewCreated() 中,我为每个按钮分配了一个 OnClickListener。我可以可靠且可预测地触发它们,但只能通过单击上图所示的窄垂直带。
我已经尝试过的解决方法和调整:
- 从
ImageViews 切换到FloatingActionButtons,最后是ImageButtons - 在不同的视图上拍
android:fitsSystemWindows="true",包括所有个 - 将按钮尺寸从
wrap_content更改为它们正在显示的VectorDrawables 中定义的明确尺寸 - 将
LinearLayout上的android:minHeight设置为与按钮相同的显式大小 - 将每个按钮的
layout_weight设为1.0,并将总和设置为3.0 - 在包含按钮的
LinearLayout上尝试app:layout_collapseMode和paralax和none。
我能在 SO 上找到的唯一类似问题是:AppBarLayout and CollapsingToolbarLayout not able to contain Button? 没有提供令人满意的答案,只是将按钮移到折叠区域之外的解决方法。
想法?
【问题讨论】:
-
也许线性布局缩小了,因为你有 android:paddingTop="32dp" android:paddingBottom="64dp -- 你可以通过在这里为布局添加颜色来测试它 android:background="@android :color/transparent" 查看它的大小——如果是这种情况,请使用边距或使用相对布局包装线性布局,例如相对布局---线性布局 --> 你的按钮
-
@Tasos 在
LinearLayout上设置android:background="@android:color/holo_red_dark"实际上会将整个工具栏布局变为红色,这不是我所期望的。但它绝对不会在按钮中间被切断。 -
好的。在 LinearLayout 中尝试这些设置,看看是否有帮助 --- android:layout_centerInParent="true" android:gravity="center_horizontal" android:orientation="vertical"
-
使其垂直,只有顶部图标的点击区域被切断。它下面的那些可以在整个可见区域上单击。显然,展开视图的布局有些奇怪。
-
我还没有测量完美像素,但看起来从状态栏底部到可点击区域顶部的区域与工具栏的高度完全相同。这一定是个线索……
标签: android android-layout material-design android-design-library