android - 背景可绘制不填充 float 操作按钮

标签 android android-drawable floating-action-button

我有 FAB 并尝试向我的整个 fab 填充一个可绘制对象 (round_drawable.xml),但可绘制对象当前显示在左上角,如此处所示,

enter image description here

我尝试了来自 Setting src and background for FloatingActionButton 的解决方案但它不起作用,添加了来自其他来源的大多数属性,但 scaleType、src、background 都不起作用。

如何使用 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

@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>

最佳答案

只需在您的 dimen.xml 文件中添加这一行

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

注意: 56dp 是 FAB 按钮的默认大小

  • 默认 - 56dp
  • 迷你 - 40dp
  • 自定义 - 您在 fabCustomSize 中指定的内容

提示:使用 app:fabCustomSize 而不是为 FAB 按钮提供自定义高度宽度。

示例

在你的布局中

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

现在创建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>

保持你的 round_drawable.xml 不变,你就可以开始了

结果

enter image description here

干杯..

关于android - 背景可绘制不填充 float 操作按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53386347/

相关文章:

android - 在圆圈内创建右箭头

java - 如何在 OK-HTTP header 中添加非 ASCII 字符串?

android - 如何更改ic_launcher_round.png文件夹内容?

javascript - navigator.language 何时会与 navigator.languages[0] 不同?

android - 在 Android 2.3 .X (Gingerbread) 及更低版本上修改 LayerDrawable 层

material-design - 出现屏幕键盘时隐藏 FAB

ionic-framework - 如何将fab按钮放在div的边缘?

Flutter,多个 float 操作按钮(有缺口)

来自字符串资源的 Android ProgressDialog setMessage

Android Studio/Eclipse 导入错误