Android: How to change size of app:fabSize=“normal” for Floating Action Button

余生长醉 提交于 2019-12-18 12:09:23

问题


When using the new FloatingActionButton, the size is determined by app:fabSize="normal". How can I set what in dp is the size referenced by "normal"?

I tried to create values/attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="app">
        <attr name="fabSize">
            <enum name="mini" value="50dp" />
            <enum name="normal" value="100dp" />
        </attr>
    </declare-styleable>
</resources>

But I get the error

"normal" in attribute "fabSize" is not a valid integer

回答1:


There are two different sizes of FAB available: normal or mini

  1. Normal (56dp) — This size should be used in most situations.

  2. Mini (40dp) — Should only be used when there is a need for visual continuity with other components displayed on the screen.




回答2:


You can override the normal and mini sizes by adding the following to values/dimens.xml:

<!-- Overriding sizes of the FAB -->
  <dimen name="design_fab_size_normal">90dp</dimen>
  <dimen name="design_fab_size_mini">30dp</dimen>

The tricky thing would be if you need more than 2 fab sizes, in that case i guess you need to create a custom view extending the fab.




回答3:


I know it's not recommended, but for those that absolutely need to change the default sizes, I was able to do it by wrapping the FloatingActionButton in a LinearLayout.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_centerInParent="true"
    android:orientation="horizontal" >

    <android.support.design.widget.FloatingActionButton
        android:layout_width="@dimen/custom_fab_size"
        android:layout_height="@dimen/custom_fab_size"
        app:fabSize="normal"
        android:clickable="true"
        android:src="@drawable/ic_mic_white_24dp"
        android:scaleType="center"/>
</LinearLayout>



回答4:


Just user app:fabCustomSize in xml

app:fabCustomSize="100dp"

Bingo.




回答5:


Standard Sizes

There are three options for standard FAB sizes (according to developer.android) which you can set them using app:fabSize.

  1. normal: The normal sized button.
  2. mini: The mini sized button.
  3. auto: Size which will change based on the window size

.

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/ic_add_black_24dp"
    app:tint="#404D54"
    app:backgroundTint="#ffd500"
    app:fabSize="normal" />

.

Custom Sizes

To set custom FAB size you can set app:fabCustomSize. Note that android:layout_width and android:layout_height should be "wrap_content".

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/ic_add_black_24dp"
    app:tint="#404D54"
    app:backgroundTint="#ffd500"
    app:fabCustomSize="36dp" />



回答6:


app:maxImageSize

<android.support.design.widget.FloatingActionButton
    android:id="@+id/floating_action_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_marginBottom="60dp"
    android:src="@drawable/icon_return_back"
    app:backgroundTint="#00ffffff"
    app:borderWidth="0dp"
    app:elevation="0dp"
    app:maxImageSize="46dp"
    app:pressedTranslationZ="0dp"
    app:rippleColor="#00ffffff" />



回答7:


<dimen name="design_fab_size_normal">90dp</dimen>
<dimen name="design_fab_size_mini">30dp</dimen>
Set this in dimen file.



回答8:


To change Fab size and to see large image init i have made below changes in android api 28:- thew are as

    <android.support.design.widget.FloatingActionButton
    android:id="@+id/profile_imageview"
    android:layout_width="@dimen/design_fab_size_mini"
    android:layout_height="@dimen/design_fab_size_mini"
    android:layout_marginTop="10dp"
    android:src="@drawable/pin_check"
    app:borderWidth="0dp"
    app:elevation="0dp"
    app:maxImageSize="90dp" />

where in dimens.xml

<dimen name="design_fab_size_mini" tools:override="true">90dp</dimen>
<dimen name="design_fab_content_size" tools:override="true">58dp</dimen>


来源:https://stackoverflow.com/questions/32882742/android-how-to-change-size-of-appfabsize-normal-for-floating-action-button

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