首页 > 解决方案 > 如何修复这个浮动按钮?

问题描述

按钮看起来很糟糕。按钮位于设备边缘,图标偏离中心。我是安卓新手。这是我的布局代码。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.mapbox.mapboxsdk.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    mapbox:mapbox_cameraZoom="15"
    mapbox:mapbox_cameraZoomMax="17"
    mapbox:mapbox_cameraZoomMin="8" >

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_location_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="320dp"
        android:layout_marginLeft="320dp"
        android:layout_marginTop="60dp"
        android:tint="@android:color/white"
        app:backgroundTint="@color/colorPrimary"
        app:srcCompat="@android:drawable/ic_search_category_default" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/status_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="320dp"
        android:layout_marginLeft="320dp"
        android:layout_marginTop="150dp"
        android:tint="@android:color/white"
        app:backgroundTint="@color/colorPrimary"
        app:srcCompat="@android:drawable/ic_menu_more" />
    </com.mapbox.mapboxsdk.maps.MapView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

我应该使用什么布局?我应该使用什么布局?我应该使用什么布局?

在此处输入图像描述

标签: androidandroid-studioandroid-layoutmapboxmapbox-android

解决方案


对于图标,解决方案是使用属性 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"/>

推荐阅读