【发布时间】:2015-01-16 13:51:25
【问题描述】:
我有一个位于两个布局之间的 FAB。使用负边距以及 android:clipChildren 和 android:clipToPadding 都设置为 false 我能够制作这个放置愉快的小按钮:
现在,问题在于 FAB 的下半部分没有接收到触摸输入,或者换句话说:不可点击。我尝试过使用android:elevation,确保它位于可能的最高 Z 值,但问题仍然存在。与此同时,上半部分运行良好。
我有点卡住了,这里有人可以帮我做什么吗?下面是我的XML 的布局,以备不时之需。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/rl_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false">
<id.ridsatrio.taggr.widgets.ObservableScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/sv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:clipChildren="false"
android:clipToPadding="false"
android:focusable="true"
android:focusableInTouchMode="true">
<id.ridsatrio.taggr.widgets.SquaredImageView
android:id="@+id/iv_details_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:contentDescription="Album Art Image"
android:elevation="@dimen/elevation_none"
android:focusable="true"
android:scaleType="centerCrop"
android:src="@drawable/def_album_art" />
<View
android:id="@+id/v_anchor"
android:layout_width="match_parent"
android:layout_height="@dimen/details_header_height"
android:layout_below="@+id/iv_details_header"
android:elevation="@dimen/elevation_low" />
<LinearLayout
android:id="@+id/ll_track_fields"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/v_anchor"
android:orientation="vertical">
...
</LinearLayout>
<View
android:id="@+id/v_divider"
android:layout_width="match_parent"
android:layout_height="@dimen/details_section_divider_height"
android:layout_below="@id/ll_track_fields"
android:layout_marginLeft="@dimen/details_section_divider_margin_start"
android:layout_marginTop="@dimen/item_horizontal_margin_xlarge"
android:background="@color/dividers_light" />
<LinearLayout
android:id="@+id/ll_album_fields"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/v_divider"
android:orientation="vertical">
...
</LinearLayout>
<RelativeLayout
android:id="@+id/rl_header"
android:layout_width="match_parent"
android:layout_height="@dimen/details_header_height"
android:layout_gravity="center_horizontal"
android:background="@color/primary"
android:clipChildren="false"
android:clipToPadding="false"
android:elevation="@dimen/elevation_low">
...
<com.melnykov.fab.FloatingActionButton
android:id="@+id/fab_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="@dimen/details_header_fab_margin_bottom"
android:layout_marginLeft="@dimen/details_header_fab_margin_start"
android:background="?android:attr/selectableItemBackground"
android:elevation="@dimen/elevation_medium"
android:src="@drawable/ic_action_down"
fab:fab_colorNormal="@color/primary_dark"
fab:fab_colorPressed="@color/primary"
fab:fab_type="mini" />
</RelativeLayout>
</RelativeLayout>
</id.ridsatrio.taggr.widgets.ObservableScrollView>
...
</RelativeLayout>
任何帮助将不胜感激。如果您需要更多详细信息,我也很乐意提供更多信息。
提前致谢!
编辑:多亏了 MoshErsan,我终于能够完成这项工作。这是我的工作XML,以防有人遇到与我相同的问题:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/rl_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false">
<id.ridsatrio.taggr.widgets.ObservableScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/sv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:clipChildren="false"
android:clipToPadding="false"
android:focusable="true"
android:focusableInTouchMode="true">
<id.ridsatrio.taggr.widgets.SquaredImageView
android:id="@+id/iv_details_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:contentDescription="Album Art Image"
android:elevation="@dimen/elevation_none"
android:focusable="true"
android:scaleType="centerCrop"
android:src="@drawable/def_album_art" />
<View
android:id="@+id/v_anchor"
android:layout_width="match_parent"
android:layout_height="@dimen/details_header_height"
android:layout_below="@+id/iv_details_header"
android:elevation="@dimen/elevation_low" />
<LinearLayout
android:id="@+id/ll_track_fields"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/v_anchor"
android:orientation="vertical">
...
</LinearLayout>
<View
android:id="@+id/v_divider"
android:layout_width="match_parent"
android:layout_height="@dimen/details_section_divider_height"
android:layout_below="@id/ll_track_fields"
android:layout_marginLeft="@dimen/details_section_divider_margin_start"
android:layout_marginTop="@dimen/item_horizontal_margin_xlarge"
android:background="@color/dividers_light" />
<LinearLayout
android:id="@+id/ll_album_fields"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/v_divider"
android:orientation="vertical">
...
</LinearLayout>
<RelativeLayout
android:id="@+id/rl_header"
android:layout_width="match_parent"
android:layout_height="@dimen/details_header_height"
android:layout_gravity="center_horizontal"
android:background="@color/primary"
android:clipChildren="false"
android:clipToPadding="false"
android:elevation="@dimen/elevation_low">
...
</RelativeLayout>
<com.melnykov.fab.FloatingActionButton
android:id="@+id/fab_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/rl_header"
android:layout_alignLeft="@+id/rl_header"
android:layout_marginBottom="@dimen/details_header_fab_margin_bottom"
android:layout_marginLeft="@dimen/details_header_fab_margin_start"
android:background="?android:attr/selectableItemBackground"
android:elevation="@dimen/elevation_medium"
android:src="@drawable/ic_action_down"
fab:fab_colorNormal="@color/primary_dark"
fab:fab_colorPressed="@color/primary"
fab:fab_type="mini" />
</RelativeLayout>
</id.ridsatrio.taggr.widgets.ObservableScrollView>
...
</RelativeLayout>
如果有什么要从这里学到的,那就是:将android:clipChilden 设置为false 将允许所述视图在其父布局之外绘制,但任何触摸输入仍将被限制在父布局的边界内。强>
【问题讨论】:
-
你能把FAB移动到根布局中的最新视图吗?我的意思是增加它的 Z 指数
-
@MoshErsan,这不是意味着额外的定位工作吗?
-
我认为问题在于,即使您确实将 FAB 带到了布局的前面,但布局仍然会消耗触摸事件,所以我仍然建议使用 FrameLayout 作为根布局并将FAB 作为其中的最后一个视图
-
并理解为什么它在 FAB 的底部没有接收到触摸事件,因为你确实使用了`android:clipChildren="false"`所以实际上父边界小于那个但是绘制发生在它的外面,所以当触摸事件首先被父接收到时,它会尝试将它转移到所需的孩子,并且触摸事件必须在父边界内,所以 FAB 的底部在边界之外父母,触摸事件不适合他
-
@MoshErsan,我明白了.. 那我该怎么办?将 FAB 作为
@+id/rl_header的兄弟姐妹(仍然是ObservableScrollView的孩子)是否足够,或者我应该将它作为@+id/rl_root的孩子一直移动到根目录?
标签: android xml android-layout button