Floating Action Button Icon not centered inside

前端 未结 8 2474
鱼传尺愫
鱼传尺愫 2021-01-02 13:07

I\'m trying to use a FAB but the icon inside is placing at bottom right of the button:

FAB with icon not centered

This is the FAB definition in the xml:

相关标签:
8条回答
  • 2021-01-02 13:14

    Please try given solution. It works for me.

    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="end|bottom"
            android:layout_margin="10dp"
            android:backgroundTint="@color/white"
            android:contentDescription="Chat"
            app:fabCustomSize="75dp"
            android:src="@drawable/chat"
            app:borderWidth="1dp"
            app:useCompatPadding="true" />
    
    0 讨论(0)
  • 2021-01-02 13:18

    This seems to be a bug in the Design Support Library v28.0.0.

    I could workarround this by setting the scaleType programmatically.

    In your case in the Java/Kotlin Code:

    centerLocationButton.setScaleType(ImageView.ScaleType.CENTER)
    
    0 讨论(0)
  • 2021-01-02 13:22

    After updating to SDK 28, I've got the same problem. Changing the size, scaleType, layout_gravity, etc will return the same.

    My current short-term solution is using the ImageButton.

    0 讨论(0)
  • Instead of android:src try app:srcCompat. Is FAB only thing you are using, because if it is (judging by the pic you posted), maybe you don't need RelativeLayout as a parent, you can use android:layout_gravity="bottom|end"

    0 讨论(0)
  • 2021-01-02 13:31

    In my situation run ok this solution app:fab_icon="@drawable/btn_back_to_top_3x" enter image description here not android:src="@drawable/btn_back_to_top_3x enter image description here

    And you can add android:scaleY="1.X" android:scaleX="1.X"

    for additional variation

    0 讨论(0)
  • 2021-01-02 13:35

    My solution is app:fabCustomSize="56dp"

    0 讨论(0)
提交回复
热议问题