[英]Floating action button's shadow clipping at view margins
I have a floating action button anchored to the bottom right of a coordinator layout.我有一个浮动操作按钮固定在协调器布局的右下角。 It is 16dp from the margins of the view (the margins are included by default and specified in the dimens.xml
file), but its shadow is clipping and has a square-shape appearance (see below).它距离视图的边距dimens.xml
(默认情况下包含边距并在dimens.xml
文件中指定),但它的阴影是剪裁的并且具有方形外观(见下文)。 When I move the floating action button to 32dp from the margins of the view, its shadow displays properly.当我将浮动操作按钮从视图边缘移动到 32dp 时,其阴影显示正确。
I have tried setting its elevation attribute ( android:elevation="5dp"
), but that seems to have no effect.我试过设置它的海拔属性( android:elevation="5dp"
),但这似乎没有效果。 I have also tried setting the attribute borderWidth to 0 ( app:borderWidth="0dp"
) but that also had no effect.我也尝试将属性 borderWidth 设置为 0( app:borderWidth="0dp"
),但这也没有效果。
Is there a reason the floating action button is behaving like this?浮动操作按钮是否有这样的行为?
XML XML
<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:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<android.support.design.widget.FloatingActionButton
android:id="@+id/create_floating_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_create_white_48dp"
app:layout_anchor="@id/coordinator_layout"
app:layout_anchorGravity="bottom|right" />
</android.support.design.widget.CoordinatorLayout>
Images图片
The problem is a parent clipping the shadow .问题是父级剪裁了 shadow 。 Look for the parent that is clipping the shadow (is not necessarily the next in the hierarchy) and add this to the view in the xml.查找正在剪裁阴影的父级(不一定是层次结构中的下一个)并将其添加到 xml 中的视图中。
android:clipChildren="false"
I've been testing this right now removing and adding that line to the parent that was clipping the view and is working fine.我现在一直在测试这个,删除并将该行添加到正在剪辑视图并且工作正常的父级。
Adding another containers or changing the margins is a workaround that I don't recommend.添加另一个容器或更改边距是我不推荐的解决方法。 It's too patchy.太片面了。 The mini fabs have different container sizes and require different sizes depending on the API level (<21 or >=21).小型晶圆厂具有不同的容器尺寸,并且根据 API 级别(<21 或 >=21)需要不同的尺寸。
Had similar issue.有类似的问题。 Make 2 things:做2件事:
In android.support.design.widget.CoordinatorLayout
remove android:paddingRight="@dimen/activity_horizontal_margin"
and android:paddingBottom="@dimen/activity_vertical_margin"
在android.support.design.widget.CoordinatorLayout
删除android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
和android:paddingBottom="@dimen/activity_vertical_margin"
In android.support.design.widget.FloatingActionButton
add android:layout_marginRight="@dimen/activity_horizontal_margin"
and android:layout_marginBottom="@dimen/activity_horizontal_margin"
在android.support.design.widget.FloatingActionButton
添加android:layout_marginRight="@dimen/activity_horizontal_margin"
和android:layout_marginBottom="@dimen/activity_horizontal_margin"
As explanation = FAB ddidn't have place to display shadow, and due to this, you have not seen it fully.作为解释= FAB ddidn't have place to display shadow,因此,您还没有完全看到它。
I too had the same problem.我也有同样的问题。 But I couldn't just throw away my margin values for the sake of the FAB
.但是我不能为了FAB
就扔掉我的保证金值。 So I added another layer in the hierarchy and this helped me get the FAB
to be placed exactly where I wanted without disrupting the parent.所以我在层次结构中添加了另一个层,这有助于我将FAB
准确放置在我想要的位置,而不会破坏父级。 So now I have a CoordinatorLayout
inside a CoordinatorLayout
for the sake of the FAB
.所以,现在我有一个CoordinatorLayout
一个内部CoordinatorLayout
了的缘故FAB
。 The below is my modified layout file.下面是我修改后的布局文件。
<android.support.design.widget.CoordinatorLayout
android:id="@+id/ddd"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/tile"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/fff"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scroll_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- All my views under a LinearLayout parent -->
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Add Text"
android:visibility="visible"
android:layout_margin="10dp"
app:backgroundTint="@color/primary"
app:layout_anchor="@+id/fff"
app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>
我已将此添加到父视图中:
android:clipToPadding="false"
I had the same issue for two ExtendedFloatingActionButton which were in a horizontal LinearLayout, so the two buttons could go next to one another.对于水平 LinearLayout 中的两个 ExtendedFloatingActionButton,我遇到了同样的问题,因此这两个按钮可以并排放置。 The grey shadow only appeared on the bottom two corners on the floating buttons but I had a bottom margin set on the linear layout so wasn't sure why there was clipping.灰色阴影只出现在浮动按钮的底部两个角上,但我在线性布局上设置了底部边距,所以不确定为什么会有剪裁。
I realised even though I had added android:clipChildren="false"
to the LinearLayout my code was wrapped in a ConstraintLayout which meant the buttons were still be cut off.我意识到即使我已经将android:clipChildren="false"
到 LinearLayout 我的代码被包裹在 ConstraintLayout 这意味着按钮仍然被切断。
To solve this issue just add android:clipChildren="false"
to the parent, which in my case was the ConstraintLayout.要解决这个问题,只需将android:clipChildren="false"
到父级,在我的例子中是 ConstraintLayout。
Example of my code:我的代码示例:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:clipToPadding="false"
android:clipChildren="false">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/btn_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="1dp"
android:text="text" />
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/btn_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.8"
android:text="text"
android:textAlignment="center" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.constraintlayout.widget.ConstraintLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.