简体   繁体   中英

Icon within FloatingActionButton is in the bottom right corner, can't seem to center it

My issue is that my floating action button's icon is in the corner when I want it in the center.

看起来是这样的:

Here is the 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_centerHorizontal="true">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/rv_select">
        </android.support.v7.widget.RecyclerView>

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/create_routine_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_add_black_24dp"
        app:fabSize="normal"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        app:backgroundTint="@color/cardAccent"
        android:layout_margin="10dp"
        app:elevation="10dp" />



</RelativeLayout>

Here is the vector I am using(I just created one using android studio):

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:autoMirrored="true" 
    android:height="24dp"
    android:tint="#FFFFFF" 
    android:viewportHeight="24.0"
    android:viewportWidth="24.0" 
    android:width="24dp">
    <path android:fillColor="#FF000000" android:pathData="M19,13h-6v6h- 
    2v-6H5v-2h6V5h2v6h6v2z"/>
</vector>

I've spent hours on this issue and would really appreciate some help!

EDIT: I figured it out... I was missing the right style

 <item name="floatingActionButtonStyle">@style/Widget.Design.FloatingActionButton</item>
  <android.support.design.widget.FloatingActionButton
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="bottom|right"
  android:layout_margin="16dp"
  app:fabSize="normal"
  app:srcCompat="@drawable/ic_add_black_24dp"
  tools:ignore="VectorDrawableCompat"
  />

//vectore file 
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
<path
    android:fillColor="#ffffffff"
    android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
</vector>

在此处输入图片说明

Use this code

1> implement this dependence

implementation 'com.google.android.material:material:1.0.0-alpha1'

2>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@drawable/vec_file" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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