简体   繁体   English

背景可绘制不填充浮动操作按钮

[英]Background drawable does not fill floating action button

I have FAB and trying to fill a drawable(round_drawable.xml) to my entire fab but the drawable appears currently as in the top left corner as shown here,我有 FAB 并试图将 drawable(round_drawable.xml) 填充到我的整个 fab 但当前 drawable 显示在左上角,如下所示,

在此处输入图片说明

I tried a solution from Setting src and background for FloatingActionButton but it did not work, added most attributes from other sources but neither scaleType, src, background did not work.我尝试了为 FloatingActionButton 设置 src 和背景的解决方案,但它不起作用,添加了来自其他来源的大多数属性,但scaleType, src, background都不起作用。

How do I get to fill the FAB with the round_drawable ?如何使用 round_drawable 填充 FAB?

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="100dp"
        android:layout_height="95dp"

        android:scaleType="fitXY"
        app:backgroundTint="@color/white"
        android:src="@drawable/round_drawable"
        android:backgroundTintMode="src_atop"

        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

round_drawable.xml round_drawable.xml

@drawable/right is a png image of right arrow. @drawable/right是右箭头的 png 图像。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape
            android:innerRadius="50dp"
            android:shape="oval">

            <corners android:radius="10dp" />

            <gradient
                android:angle="45"
                android:endColor="#D425B5"
                android:startColor="#EBF928" />

        </shape>
    </item>
    <item
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/right" />
    </item>
</layer-list>

Just add this line in you dimen.xml file只需在dimen.xml文件中添加这一行

<dimen name="design_fab_image_size" tools:override="true">56dp</dimen>

NOTE : 56dp is default size of FAB buttom注意: 56dp是 FAB 按钮的默认尺寸

  • Default - 56dp默认 - 56dp
  • mini - 40dp迷你 - 40dp
  • custom - whatever you have specified in fabCustomSize custom - 您在fabCustomSize指定的fabCustomSize

TIP : use app:fabCustomSize rather giving custom height width to FAB button.提示:使用app:fabCustomSize而不是为 FAB 按钮提供自定义高度宽度。

Example例子

in your layout在你的布局中

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="@color/white"

    <!--inmprtent-->
    app:fabCustomSize="100dp"

    android:src="@drawable/round_drawable"
    android:backgroundTintMode="src_atop"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>

now create dimens.xml现在创建dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
    <dimen name="design_fab_image_size" tools:override="true">100dp</dimen>
</resources>

keep your round_drawable.xml as it is and you are good to go保持你的round_drawable.xml原样,你很高兴

Result结果

在此处输入图片说明

cheers..干杯..

You don't have to give specific size to FAB.And it won't require background drawable also for any specific color.您不必为 FAB 指定特定大小。它也不需要任何特定颜色的背景可绘制。 It has attribute fabSize in which you can pass predefined sizes like mini(40dp), normal(56dp) .它具有fabSize属性,您可以在其中传递预定义的大小,例如mini(40dp), normal(56dp) And still if you want to add size remove sizes from drawable <item> tag.而且,如果您想从可绘制的<item>标签中添加尺寸,请删除尺寸。 That tag actually making your drawable small.该标签实际上使您的 drawable 变小了。

check this sample code for creating Fab with image in center检查此示例代码以创建带有中心图像的 Fab

<android.support.design.widget.FloatingActionButton
            android:id="@+id/fbtn_addNotification"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="@dimen/margin_16"
            android:layout_marginRight="@dimen/margin_16"
            app:srcCompat="@drawable/ic_add_white_28dp"
            app:backgroundTint="@color/colorPrimary"
            app:borderWidth="@dimen/margin_0"
            app:elevation="@dimen/margin_8"
            app:fabSize="normal"
            android:visibility="visible"
            app:pressedTranslationZ="@dimen/margin_5" />

It will look like this.它看起来像这样。 Hope it will help...希望它会有所帮助...

在此处输入图片说明

I used this code and it worked:我使用了这段代码并且它有效:

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/add"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_add"
        android:scaleType="center"
        android:backgroundTint="@android:color/transparent"
        android:outlineAmbientShadowColor="@android:color/transparent"
        android:outlineSpotShadowColor="@android:color/transparent"
      />

and in the dimens.xml file: <dimen name="design_fab_image_size" tools:override="true">40dp</dimen>并在 dimens.xml 文件中: <dimen name="design_fab_image_size" tools:override="true">40dp</dimen> dimen <dimen name="design_fab_image_size" tools:override="true">40dp</dimen>

Updated answer of 2019 with androidX:使用 androidX 更新了 2019 年的答案:

Dependency : implementation 'com.getbase:floatingactionbutton:1.10.1'依赖: implementation 'com.getbase:floatingactionbutton:1.10.1'

XML: XML:

<com.getbase.floatingactionbutton.FloatingActionsMenu
    android:id="@+id/multiple_actions"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_margin="@dimen/fab_margin"
    android:layout_marginBottom="20dp"
    app:fab_addButtonColorNormal="@color/colorPrimaryDark"
    app:fab_addButtonColorPressed="@color/colorPrimaryDark"
    app:fab_colorPressed="@color/colorPrimaryDark"
    app:fab_labelStyle="@style/menu_labels_style">

    <com.getbase.floatingactionbutton.FloatingActionButton
        android:id="@+id/btn_langauge_translation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_language_icon"
        app:fab_size="normal"
        android:paddingBottom="15dp"
        app:fab_colorNormal="@color/colorPrimaryDark"
        app:fab_colorPressed="@color/colorPrimaryDark"
        app:fab_title="" />

    <com.getbase.floatingactionbutton.FloatingActionButton
        android:id="@+id/btn_facebook_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_fb_icon"
        app:fab_size="normal"
        android:paddingBottom="15dp"
        app:fab_colorNormal="#0184FF"
        app:fab_colorPressed="#0184FF"
        app:fab_title="" />

    <com.getbase.floatingactionbutton.FloatingActionButton
        android:id="@+id/btn_whats_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_whatsapp_icon"
        app:fab_colorNormal="#4EC248"
        android:paddingBottom="15dp"
        app:fab_size="normal"
        app:fab_colorPressed="#4EC248"
        app:fab_title="" />

    <com.getbase.floatingactionbutton.FloatingActionButton
        android:id="@+id/btn_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_email_icon"
        app:fab_colorNormal="#848484"
        android:paddingBottom="15dp"
        app:fab_colorPressed="#848484"
        app:fab_size="normal"
        app:fab_title="" />

</com.getbase.floatingactionbutton.FloatingActionsMenu>

Output:输出:

在此处输入图片说明

Just add this line in you style.xml file只需在 style.xml 文件中添加这一行

<style name="FullImageFloatingButton" parent="Widget.Design.FloatingActionButton">
    <item name="fabSize">normal</item>
    <item name="maxImageSize">56dp</item>
</style>

<style name="FullImageMiniFloatingButton" parent="Widget.Design.FloatingActionButton">
    <item name="fabSize">mini</item>
    <item name="maxImageSize">40dp</item>
</style>

Use FullImageFloatingButton for normal FloatingButton and FullImageMiniFloatingButton for mini FloatingButton.使用FullImageFloatingButton正常FloatingButton和FullImageMiniFloatingButton迷你FloatingButton。

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

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