[英]Floating Action Button in Android having icon not in center
這是XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Hello World!" />
<android.support.design.widget.FloatingActionButton
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="4dp"
android:scaleType="center"
android:src="@drawable/button_floating_add"
app:borderWidth="0dp"
app:elevation="8dp" />
</RelativeLayout>
輸出看起來像這樣:
我認為這是由於API版本的更改而發生的。 我正在使用compileSDK和targetSDK版本28
,以及設計和appcompat庫28.0.0
。 當我將API和設計庫版本更改回26
,輸出如下所示:
FAB容器有兩種尺寸:
- 默認值(56 x 56dp)
- 迷你(40 x 40dp)
因此,您要使用小型FAB。 但是您不能使用layout_width
和layout_height
定義它,因為這只會弄亂填充。 您必須使用app:fabSize
屬性並將其設置為SIZE_MINI
:
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="mini"
... />
您是否嘗試過使用Android Asset Studio? 它們幾乎具有每種尺寸和密度的每個基本可繪制對象。 繼承人鏈接https://developer.android.com/studio/write/image-asset-studio.html
根據視圖的高度和寬度使用此屬性
app:fabCustomSize="40dp"
並建議您使用.svg文件而不是.png文件。
您可以嘗試這些屬性,
android:layout_gravity="bottom|center"
android:layout_centerHorizontal="true"
android:scaleType="fitCenter"
晶圓廠是一種特殊的視圖,您必須遵守其所需的需求。 話雖這么說,其寬度和高度應該是"wrap_content"
。 如果您希望自己的fab為迷你版,則可以添加app:fabSize="mini"
。 您可以使用layout_gravity
設置其屏幕位置(在下面的示例中,該位置位於右下角)。 另外,圖標必須為24dp x 24dp。 我建議您從https://material.io/tools/icons/?style=baseline下載圖標,在這里您可以選擇圖像大小以及所有其他內容。
<com.google.android.material.floatingactionbutton.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:onClick="@{() -> viewModel.onFabPressed()}"
app:srcCompat="@drawable/ic_baseline_add_24px"/>
上面的xml是我的一個應用程序的工作示例。 我希望這會有所幫助,並祝您有美好的一天!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.