简体   繁体   English

调整浮动操作按钮的图标大小(fab)

[英]Adjust icon size of Floating action button (fab)

浮动按钮The new floating action button should be 56dp x 56dp and the icon inside it should be 24dp x 24dp .新的浮动操作按钮应为56dp x 56dp ,其中的图标应为24dp x 24dp So the space between icon and button should be 16dp .所以 icon 和 button 之间的间距应该是16dp

<ImageButton
    android:id="@+id/fab_add"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="16dp"
    android:layout_marginRight="16dp"
    android:background="@drawable/ripple_oval"
    android:elevation="8dp"
    android:src="@drawable/ic_add_black_48dp" />

ripple_oval.xml ripple_oval.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item>
        <shape android:shape="oval">
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>

And this is the result I get:这是我得到的结果:
浮动按钮结果
I used the icon from \material-design-icons-1.0.0\content\drawable-hdpi\ic_add_black_48dp.png我使用了\material-design-icons-1.0.0\content\drawable-hdpi\ic_add_black_48dp.png 中的图标
https://github.com/google/material-design-icons/releases/tag/1.0.1 https://github.com/google/material-design-icons/releases/tag/1.0.1

How to make the size of the icon inside the button be exactly as described in guidelines?如何使按钮内的图标大小与指南中描述的完全一致

http://www.google.com/design/spec/components/buttons.html#buttons-floating-action-button http://www.google.com/design/spec/components/buttons.html#buttons-floating-action-button

As your content is 24dp x 24dp you should use 24dp icon .由于您的内容是 24dp x 24dp,您应该使用24dp icon And then set android:scaleType="center" in your ImageButton to avoid auto resize.然后在 ImageButton 中设置android:scaleType="center"以避免自动调整大小。

将支持库更新到 v28.0.0 后,尝试使用app:maxImageSize="56dp"而不是上述答案

Make this entry in dimens在维度中输入此条目

<!--Floating action button-->
<dimen name="design_fab_image_size" tools:override="true">36dp</dimen>

Here 36dp is icon size on floating point button.这里 36dp 是浮点按钮上的图标大小。 This will set 36dp size for all icons for floating action button.这将为浮动操作按钮的所有图标设置 36dp 大小。

Updates As Per Comments根据评论更新

If you want to set icon size to particular Floating Action Button just go with Floating action button attributes like app:fabSize="normal" and android:scaleType="center".如果您想将图标大小设置为特定的浮动操作按钮,只需使用浮动操作按钮属性,例如 app:fabSize="normal" 和 android:scaleType="center"。

  <!--app:fabSize decides size of floating action button You can use normal, auto or mini as per need-->
  app:fabSize="normal" 

  <!--android:scaleType decides how the icon drawable will be scaled on Floating action button. You can use center(to show scr image as original), fitXY, centerCrop, fitCenter, fitEnd, fitStart, centerInside.-->
  android:scaleType="center"

There are three key XML attributes for custom FABs:自定义 FAB 有三个关键的 XML 属性:

  • app:fabSize : Either "mini" (40dp), "normal"(56dp) (default) or "auto" app:fabSize :“迷你”(40dp)、“普通”(56dp) (默认)或“自动”
  • app:fabCustomSize : This will decide the overall FAB size. app:fabCustomSize :这将决定整体 FAB 大小。
  • app:maxImageSize : This will decide the icon size. app:maxImageSize :这将决定图标大小。

Example:例子:

app:fabCustomSize="64dp" 
app:maxImageSize="32dp"

The FAB padding (the space between the icon and the background circle, aka ripple ) is calculated implicitly by: FAB 填充(图标和背景圆圈之间的空间,又名涟漪)通过以下方式隐式计算:

4-edge padding = (fabCustomSize - maxImageSize) / 2.0 = 16

Note that the margins of the fab can be set by the usual android:margin xml tag properties.请注意,fab 的边距可以通过通常的android:margin xml 标签属性设置。

The design guidelines defines two sizes and unless there is a strong reason to deviate from using either, the size can be controlled with the fabSize XML attribute of the FloatingActionButton component.设计指南定义了两种大小,除非有充分的理由不使用任何一种,否则可以使用FloatingActionButton组件的fabSize XML 属性来控制大小。

Consider specifying using either app:fabSize="normal" or app:fabSize="mini" , eg:考虑使用app:fabSize="normal"app:fabSize="mini" ,例如:

<android.support.design.widget.FloatingActionButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/ic_done_white_24px"
   app:fabSize="mini" />
<ImageButton
        android:background="@drawable/action_button_bg"
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:padding="16dp"
        android:src="@drawable/ic_add_black_48dp"
        android:scaleType="fitXY"
        android:elevation="8dp"/>

With the background you provided it results in below button on my device (Nexus 7 2012)使用您提供的背景,我的设备上会显示以下按钮(Nexus 7 2012)

在此处输入图片说明

Looks good to me.在我看来很好。

What's your goal?你的目标是什么?

If set icon image size to bigger one:如果将图标图像大小设置为更大的:

  1. Make sure to have a bigger image size than your target size (so you can set max image size for your icon)确保图像尺寸大于目标尺寸(so you can set max image size for your icon)

  2. My target icon image size is 84dp & fab size is 112dp :我的目标图标图像大小为84dp ,晶圆厂大小为112dp

     <android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src= <image here> app:fabCustomSize="112dp" app:fabSize="auto" app:maxImageSize="84dp" />

If you are using androidx 1.0.0 and are using a custom fab size, you will have to specify the custom size using如果您使用的是 androidx 1.0.0 并使用自定义 fab 大小,则必须使用指定自定义大小

app:fabCustomSize="your custom size in dp"

By deafult the size is 56dp and there is another variation that is the small sized fab which is 40dp, if you are using anything you will have to specify it for the padding to be calculated correctly默认情况下,尺寸为 56dp,还有另一种变体,即 40dp 的小型晶圆厂,如果您使用任何东西,则必须指定它才能正确计算填充

You can play with the following settings of FloatingActionButton : android:scaleType and app:maxImageSize.您可以使用 FloatingActionButton 的以下设置:android:scaleType 和 app:maxImageSize。 As for me, I've got a desirable result if android:scaleType="centerInside" and app:maxImageSize="56dp".至于我,如果 android:scaleType="centerInside" 和 app:maxImageSize="56dp",我就得到了理想的结果。

As FAB is like ImageView so You can use scaleType attribute to change the icon size likewise ImageView .由于FAB就像ImageView所以你可以使用scaleType属性来改变ImageView的图标大小。 Default scaleType for a FAB is fitCenter . FAB默认scaleTypefitCenter You can use center and centerInside to make icon small and large respectively.您可以使用centercenterInside分别使图标变小和变大。

All values for scaleType attribute are - center, centerCrop, centerInside, fitCenter, fitEnd, fitStart, fitXY and matrix . scaleType属性的所有值是 - center, centerCrop, centerInside, fitCenter, fitEnd, fitStart, fitXYmatrix

See https://developer.android.com/reference/android/widget/ImageView.ScaleType.html for more details.有关更多详细信息,请参阅https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

关闭@IRadha 在矢量可绘制设置中的答案android:height="_dp"并将缩放类型设置为android:scaleType="center"将可绘制设置为设置的任何大小

浮动按钮The new floating action button should be 56dp x 56dp and the icon inside it should be 24dp x 24dp .新的浮动操作按钮应为56dp x 56dp ,其内部的图标应为24dp x 24dp So the space between icon and button should be 16dp .因此,图标和按钮之间的间距应为16dp

<ImageButton
    android:id="@+id/fab_add"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="16dp"
    android:layout_marginRight="16dp"
    android:background="@drawable/ripple_oval"
    android:elevation="8dp"
    android:src="@drawable/ic_add_black_48dp" />

ripple_oval.xml波纹_oval.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item>
        <shape android:shape="oval">
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>

And this is the result I get:这是我得到的结果:
浮动按钮结果
I used the icon from \\material-design-icons-1.0.0\\content\\drawable-hdpi\\ic_add_black_48dp.png我使用了\\ material-design-icons-1.0.0 \\ content \\ drawable-hdpi \\ ic_add_black_48dp.png中的图标
https://github.com/google/material-design-icons/releases/tag/1.0.1 https://github.com/google/material-design-icons/releases/tag/1.0.1

How to make the size of the icon inside the button be exactly as described in guidelines ?如何使按钮内的图标大小完全符合准则中所述?

http://www.google.com/design/spec/components/buttons.html#buttons-floating-action-button http://www.google.com/design/spec/components/buttons.html#buttons-floating-action-button

Using ExtendedFloatingActionButton, this solved the issue for me:使用 ExtendedFloatingActionButton,这为我解决了这个问题:

app:iconSize="<x>dp"

I've tried iconPadding or fabCustomSize but I haven't seem any changes我试过iconPaddingfabCustomSize但我似乎没有任何变化

You can easily set your FAB icon size by using,您可以使用以下方法轻松设置 FAB 图标大小,

final FloatingActionButton button = new FloatingActionButton(context);最终 FloatingActionButton 按钮 = 新的 FloatingActionButton(context);

button.setCustomSize(50); button.setCustomSize(50);

    android:id="@+id/floatingActionButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:clickable="true"
    app:backgroundTint="#FBFAFA"
    app:fabCustomSize="70dp"
    app:fabSize="mini"
    app:maxImageSize="84dp"
    app:srcCompat="@drawable/resim"
    app:tint="#E81C1C"
    tools:layout_editor_absoluteX="325dp"
    tools:layout_editor_absoluteY="517dp" />

I Handled It This Way我是这样处理的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM