【问题标题】:Floating Action Button forcing image to bottom right corner instead of center浮动操作按钮强制图像到右下角而不是中心
【发布时间】:2019-11-28 13:48:08
【问题描述】:

我正在使用带有图像的浮动操作按钮,并且在最近一次 Android Studio 更新后,图像不再居中并位于右下角

我从未更改任何有关图像或按钮的代码,但我很好奇如何才能让它再次居中。

这是我的按钮:

<android.support.design.widget.FloatingActionButton
 android:id="@+id/expandButton"
 android:layout_width="44dp"
 android:layout_height="44dp"
 android:layout_alignParentEnd="true"
 android:layout_below="@id/imageFrame"
 android:layout_marginBottom="-22dp"
 android:layout_marginEnd="@dimen/item_margin"
 android:layout_marginTop="-22dp"
 android:background="@color/orange"
 app:backgroundTint="@color/orange"
 app:borderWidth="0dp"
 app:srcCompat="@drawable/ic_expand" />

这是图片:

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="512dp"
    android:height="512dp"
    android:viewportWidth="512"
    android:viewportHeight="512">

    <path
        android:fillColor="@color/white"
        android:pathData="M64.9 127c-14.9-14.9-39.1-14.9-54 0s-14.9 39.1 0 54l218 218c14.9 14.9 39.1 14.9 54 0l218-218c14.9-14.9 14.9-39.1 0-54-14.9-14.9-39.1-14.9-54 0l-191 191.2L64.9 127z" />
</vector>

正如我所说,我从未对这两个部分进行任何更改,现在突然之间,图像被强制到右下角。如何让图片居中在按钮上?

【问题讨论】:

    标签: android android-layout android-button


    【解决方案1】:

    我已经尝试过您的代码。我所做的只是更改了一些属性。

    对于 FloatingActionButton。

    尝试改变这个

    android:layout_width="44dp"
    android:layout_height="44dp"
    

    到这里

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    

    对于 ic_expand

    改变这个

    android:viewportWidth="512"
    android:viewportHeight="512"
    

    到这里

    android:viewportWidth="24.0"
    android:viewportHeight="24.0"
    

    让我知道这是否有效。

    [编辑] 这是我的代码。

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="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"
        android:gravity="center">
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/expandButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="-22dp"
            android:layout_marginBottom="-22dp"
            app:borderWidth="0dp"
            app:srcCompat="@drawable/ic_expand_more_black_24dp" />
    
    </RelativeLayout>
    

    ic_expand_more_black_24dp.xml

    <vector
        android:height="512dp"
        android:viewportHeight="24"
        android:viewportWidth="24"
        android:width="512dp"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <path android:fillColor="#FF000000"
            android:pathData="M16.59,8.59L12,13.17 7.41,8.59 6,10l6,6 6,-6z"/>
    </vector>
    

    这里是image

    【讨论】:

    • 不幸的是,这不起作用,它甚至根本不显示图像。
    • @BlondeSwan 您可以尝试将 FloatingActionButton 的值更改为 wrap_content。并将 ic_expand 的初始值保持为 512。
    【解决方案2】:

    设置

     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
    

    请尝试以下代码:

    <android.support.design.widget.FloatingActionButton
            android:id="@+id/expandButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@drawable/ic_expand"
            app:backgroundTint="@color/orange"/>
    

    我希望它对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 2023-03-23
      • 2020-11-15
      • 2020-07-17
      • 1970-01-01
      相关资源
      最近更新 更多