Android:如何更改app的大小:fabSize =“正常”的浮动操作按钮

时间:2015-10-01 08:21:37

标签: android android-layout floating-action-button

使用新的FloatingActionButton时,尺寸由app:fabSize="normal"确定。如何设置dp"normal"引用的大小的内容是什么?

我尝试创建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>

但是我收到了错误

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

9 个答案:

答案 0 :(得分:37)

您可以通过将以下内容添加到values/dimens.xml

来覆盖普通和迷你尺寸
<!-- Overriding sizes of the FAB -->
  <dimen name="design_fab_size_normal">90dp</dimen>
  <dimen name="design_fab_size_mini">30dp</dimen>

如果您需要超过2个晶圆厂尺寸,那将是棘手的事情,在这种情况下,我猜您需要创建一个扩展晶圆厂的自定义视图。

答案 1 :(得分:35)

有两种不同尺寸的FAB可用:normalmini

  1. Normal (56dp) - 此大小应在大多数情况下使用。

  2. Mini (40dp) - 只有在需要与屏幕上显示的其他组件保持视觉连续性时才能使用。

答案 2 :(得分:16)

我知道不推荐这样做,但对于那些绝对需要更改默认尺寸的人,我可以将FloatingActionButton包裹在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>

答案 3 :(得分:10)

只是用户app:xml中的fabCustomSize

app:fabCustomSize="100dp"

宾果

答案 4 :(得分:5)

Standard Sizes

标准FAB大小(根据developer.android)有三个选项,您可以使用app:fabSize进行设置。

  1. 常规:常规尺寸的按钮。
  2. mini :迷你按钮。
  3. 自动:大小将根据窗口大小而改变

<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

要设置自定义FAB的大小,可以设置app:fabCustomSize。请注意,android:layout_widthandroid:layout_height应该是"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" />

答案 5 :(得分:1)

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" />

答案 6 :(得分:0)

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

答案 7 :(得分:0)

要更改Fab大小并查看大图像初始化,我在android api 28中做了以下更改:-

    <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" />

dimens.xml中的

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

答案 8 :(得分:0)

只需在FAB组件下的xml文件中添加app:fabCustomSize="70dp"

以下是FAB按钮的示例:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab_AddRoom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:src="@android:drawable/ic_input_add"
    app:fabCustomSize="70dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.93"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.93" />