我有FAB并尝试将drawable(round_drawable.xml)填充到我的整个工厂,但是drawable目前显示在左上角,如此处所示,
我尝试了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>
注意:qazxsw poi是FAB按钮的默认大小
56dp
中指定了什么提示:使用fabCustomSize
而不是给FAB按钮提供自定义高度宽度。
例
在你的布局中
app:fabCustomSize
现在创建<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
干杯..
您不必为FAB指定特定尺寸。对于任何特定颜色,它也不需要背景可绘制。它具有属性fabSize,您可以在其中传递预定义的大小,如mini(40dp),normal(56dp)。如果你想添加尺寸从drawable 标签删除大小仍然。那个标签实际上让你的绘画很小。
检查此示例代码以创建具有中心图像的Fab
<item>
它看起来像这样。希望它会有所帮助......
<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" />