Elevation not working on android.support.design.widget.FloatingActionButton

三世轮回 提交于 2019-12-10 14:31:47

问题


I've just started android development and trying the new material design.

is a screenshot of my mainactivity which has a FloatingActionButton but it doesnt applying any elevation (no shadows).

How can I enable shadow on this new widget (android.support.widget.FloatingActionButton).

This is code from layout xml

<android.support.design.widget.FloatingActionButton
        android:id="@+id/add_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_add_action"
        android:layout_margin="16dp"
        android:elevation="10dp"
        android:padding="10dp"/>

Any help appreciated. Thanks

Please note I'd like to use android design library only not any other github libraries.


回答1:


After a lot of research, I've found the proper way to use FAB with no issues. Use the code below as template:

<android.support.design.widget.FloatingActionButton
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/your_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="@dimen/floating_button_margin_bottom"
        android:layout_marginRight="@dimen/floating_button_margin_right"
        app:elevation="@dimen/floating_button_elevation"
        app:borderWidth="0dp"
        app:rippleColor="@color/your_ripple_color"
        app:backgroundTint="@color/your_bg_color" />



回答2:


I'm just testing the floating action button now. And for me it works.

Try adding app:borderWidth="0dp" for your button, this might solve the problem. I saw in some other posts that it might be a problem in the design library.




回答3:


Your XML layout might missed these code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   app:xmlns="http://schemas.android.com/apk/res-auto"
   ... >
         <android.support.design.widget.FloatingActionButton
            ... 
            app:borderWidth="0dp"
            app:elevation="4dp" />
   ... 
</RelativeLayout>

EDIT

For more information about FloatingActionButton, see this post, which explains the issue and design guide.




回答4:


Check the manifest file and delete the next properties:

 android:hardwareAccelerated="false"
 android:largeHeap="true"


来源:https://stackoverflow.com/questions/30739535/elevation-not-working-on-android-support-design-widget-floatingactionbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!