我有 FAB 并尝试向我的整个 fab 填充一个可绘制对象 (round_drawable.xml),但可绘制对象当前显示在左上角,如此处所示,
我尝试了来自 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
不变,你就可以开始了
结果
干杯..
关于android - 背景可绘制不填充 float 操作按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53386347/