Why does FAB appear different on different Android

2019-07-11 03:36发布

问题:

Can anyone tell me why exactly the same image (same image resource file as PNG, and same XML definition below) looks perfect on Android 5.1 and 7.1.1 (the image above) but bad on Android 4.1 (the image below).

The above image appears perfect on: Samsung real device 5.1 Pixel API 25 (7.1.1) emulator

The below image looks bad on: Samsung real device 4.3 Nexus One API 18 4.2 emulator

The XML code:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/myFAB"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:layout_gravity="center|bottom"
    android:layout_margin="@dimen/fab_margin"
    android:visibility="invisible"/>

Please advice how can I fix this problem.

EDIT: the images were generated from Android Studio "New Image Asset" and different resolutions (hdpi, mdpi, xhdpi, xxhdpi) has indeed been generated.

回答1:

FloatingActionButton cannot take any width/height. app:fabSize parameters specifies 3 sizes for the fab: auto, mini, and normal.

Leave layout_width and layout_height as wrap_content, and specify the desired fab size using app:fabSize="normal" (or other parameter from the list).