为什么我的浮动操作按钮不能正确显示其图像?
This is what my floating action button is showing为什么我的浮动操作按钮不能正确显示其图像?
宁可只是显示图像的实际圆形部分,它会尝试使我的.png文件的背景适合浮动动作按钮。
这里是我的XML代码:
<android.support.design.widget.FloatingActionButton
android:id="@+id/addToKitchenButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_gravity="bottom|end"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:src="@drawable/fab_icon"
/>
how do I get it to look like this?
我已经给我的PNG文件透明背景,所以我不知道还有什么我需要做的。请帮忙。
谢谢。
(假设您使用的是Android Studio) - 不是使用您自己的自定义.png作为FloatingActionButton的src,而是使用该图标创建一个新的矢量素材。在Android Studio中右键单击您的res/drawable文件夹,然后单击新建 - >矢量资产。
您使用的是内置于Android的工作室,虽然你可以导入自己的矢量资源,如果你正在做一些更多的自定义图标。它生成XML将看起来像这样:
<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="#FF000000"
android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
,你可以更改填充颜色的颜色。在android:src中将你的FloatingActionButton设置为这个可绘制的,你很好走。
<android.support.design.widget.FloatingActionButton
android:id="@+id/addFolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/plus"
app:layout_anchor="@id/mainRl"
android:background="#ff0000"
app:layout_anchorGravity="bottom|right|end"
android:onClick="onAddFolderClick"
/>
我使用#FF0000(红色)作为背景颜色,你用你想要的粉红色。这对我来说可以。
为了保持一致的外观,您需要做两件事。 这个回答假设你真的想在链接的外观由您提供的是this
第一步 - 的背景颜色是什么,但你的应用程序的口音颜色,你不需要提供任何特定的颜色。所以首先确保你的应用主题设置了重音颜色。
第二步 - 完成之后,您需要使用下面的xml代码。 如果你想要看链接,你甚至不需要创建一个新的PNG。您可以从this official google material icons link轻松下载白色添加图标。您可以通过谷歌使用这个链接的大多数标准材料图标。
<android.support.design.widget.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"
android:src="@drawable/ic_add_white_24dp"/>
希望这有助于。请让我知道它是否改变了你的事情。
编辑::如果你想改变图标的颜色和背景的东西比你的默认口音的颜色完全不同,那么你必须要使用这些属性 -
//自定义背景
app:backgroundTint="@color/yourCustomColor"
//自定义图标的颜色
android:tint="@color/yourCustomColor"
谢谢你这个工作,但现在我有这个问题http://stackoverflow.com/questions/39737699/why-is-my-floating-action-button-not-displaying-properly不同的问题从m它不会正确显示 –
谢谢,这正是我所需要的,但现在我有这个问题http://stackoverflow.com/questions/39737699/why-is-my-floating-action-button-not-displaying-properly-different-问题从m它不能正常显示 –