繁体   English   中英

浮动操作按钮将图像强制移到右下角而不是中心

[英]Floating Action Button forcing image to bottom right corner instead of center

我将浮动操作按钮与图片一起使用,并且在最近进行了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>

正如我所说的那样,我从未对这两个部分进行任何更改,现在图像突然被迫移到右下角。 如何使图像居中显示在按钮上?

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

对于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>

这是图片

 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"/>

希望它对您有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM