Android: Create bigger Floating Action Button with bigger icon

前端 未结 8 516
陌清茗
陌清茗 2021-02-02 11:02

I want to create a bigger Floating Action Button (FAB), more than the usual 56 dp, with a bigger icon inside. (For my purpose an icon will never be self-explanatory so I will cr

8条回答
  •  借酒劲吻你
    2021-02-02 11:38

    Since Design Support Library 27.1.0 there is an official way to set custom FAB size: app:fabCustomSize XML attribute and corresponding setCustomSize method. For increasing icon size you can use android:scaleType="center" and larger drawable. UPDATE: android:scaleType is now broken, use fab.setScaleType(ImageView.ScaleType.CENTER) instead.

    Other proposed solutions have some drawbacks:

    • Redefining default FAB dimensions cannot be applied to single button and is essentially a hack (uses non-public API which is subject to change).
    • Wrapping FAB in FrameLayout (or just setting FAB's layout_width and layout_height to desired size, which has the same effect) looks awfully on pre-Lollipop devices. UPDATE: Now on post-Lollipop too (see screenshots below).
    • Setting scaleX and scaleY requires adjusting margins (because visual FAB size now does not match its layout size) and scales non-scalable parts: button border and (pre-Lollipop) shadow width.

    Sample layout as rendered on API 19 (left) and API 21 (right):

提交回复
热议问题