Android:浮动按钮图标不居中

时间:2017-01-05 03:47:54

标签: android floating-action-button

我试图在我的应用中创建浮动按钮。按钮在那里,但按钮中的图像有点向上(见图像)。

enter image description here

我无法弄清楚它有什么问题。下面是浮动按钮的XML的一部分。

<android.support.design.widget.FloatingActionButton
        android:id="@+id/buttonUp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_marginTop="480dp"
        android:clickable="true"
        android:scaleType="center"
        android:src="@drawable/btn_back_to_top_3x"
        app:layout_anchor="@id/layout"
        app:layout_anchorGravity="bottom|right|end"
        app:backgroundTint="@android:color/background_light"
        app:fabSize="normal" />

按钮显示我想要的位置。唯一的问题似乎是里面的图像不是中心。为什么会这样?

修改: 经过一番观察,我意识到图像本身存在问题,图像实际上并不居中,底部有空间(导致图像向上推)。

11 个答案:

答案 0 :(得分:43)

我也遇到了同样的问题。以下解决方案对我有用

app:fabCustomSize="40dp"

答案 1 :(得分:10)

您应该将FabCustomSize值设置为类似于FAB的大小:

<android.support.design.widget.FloatingActionButton
        android:layout_width="60dp"
        android:layout_height="60dp"
        app:fabCustomSize="60dp"
        android:src="@drawable/plus_icon"
        app:rippleColor="@color/colorPrimaryDark" />

答案 2 :(得分:4)

您只需添加app:fabCustomSize="40dp"即可为我工作

答案 3 :(得分:1)

我只设置了app:fabCustomSize =“ 40dp”(所需大小),并将高度和宽度设置为wrap_content,然后问题解决了

答案 4 :(得分:0)

尝试修复你的身高和宽度......

    android:layout_width="50dp"
    android:layout_height="50dp"

请另外以另一种方式调整你的布局..因为这里你做了

 android:layout_marginTop="480dp" 

我的建议尝试将FloatingActionButton设置在Relative Layout ...并设置为此属性,这不是一个正确的方法..

    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"

答案 5 :(得分:0)

使用(https://github.com/futuresimple/android-floating-action-button)并设置fab:fab_icon =&#34; @ drawable / ic_fab_star&#34;中心图标

答案 6 :(得分:0)

这通常在使用自定义大小的浮动操作按钮(FAB)时出现,因为android默认情况下仅支持40dp FAB。最好的方法是覆盖它,方法是告诉您您正在使用自定义大小的FAB,并为其提供与值相同的大小。

例如,在使用60dp x 60dp FAB时,就可以解决问题。

app:fabCustomSize="60dp"

答案 7 :(得分:0)

这里是关于设置三个选项一起调整导航按钮和图标大小

  1. 布局宽度和高度。

  2. 更改导航按钮内的图标大小 ~ 使用 (maxImageSize="..dp")。

  3. 改变晶圆厂尺寸~使用 (app:fabCustomSize="..dp") OR (app:fabSize="") 你可以在这里尝试三个选项(“mini”或“auto”或“normal”) . 例如:

    <com.google.android.material.floatingactionbutton.FloatingActionButton
     android:layout_width="40dp"
     android:layout_height="40dp"
     app:maxImageSize="20dp"
     app:fabSize="mini"/>
    

答案 8 :(得分:0)

除了上述答案之外,无论您为 FAB 设置什么尺寸,都将其设置为 fabCustomSize

例如,如果宽度和高度为“48dp”,则将 fabCustomSize 设置为“48dp”:​​

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:backgroundTint="@color/grey"
    android:src="@drawable/ic_baseline_arrow_back"
    app:fabCustomSize="48dp" />

答案 9 :(得分:-1)

最好的解决方案是不对FAB的尺寸进行硬编码。

图标将以属性android:scaleType="center"居中

<android.support.design.widget.FloatingActionButton
                android:id="@+id/search_fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="center"
                android:layout_marginEnd="10dp"
                android:src="@drawable/fab_search_icon"
                app:backgroundTint="@color/colorPrimary"
                app:elevation="4dp"
                app:fabSize="normal"/>

答案 10 :(得分:-3)

原因是你的影像尺寸不合适!!

浮动操作按钮默认圆圈大小为56 x 56dp , 为了你的背景图像,最适合使用!!

如果你想要一个小型的应该是:40 x 40dp

如果您只想更改内部图标(仅图标),请使用24 x 24dp图标作为默认尺寸

为了再次测试,我下载了一个图像形式的互联网,并将其缩小到56dp并保留一个小空间来平衡它(我对Photoshop不好)

enter image description here

并将其添加到您的FB视图中并将背景设为黄色以使其清晰。请参阅

out put:

enter image description here

这意味着它取决于您添加的图片及其尺寸,如果您想要全背景,甚至可以使用imageButton

参考:https://material.io/guidelines/components/buttons-floating-action-button.html#buttons-floating-action-button-floating-action-button