![](/img/trans.png)
[英]Android : How to set gravity of Floating Action Button Programmatically?
[英]Android Floating Action Button wrong position despite gravity is set
我目前面臨兩個不同的問題,都與晶圓廠的重力行為有關。
在我的布局中,我有一個由FrameLayout
組成的部分,其中有一個ImageView
和一個FloatingActionButton
作為子視圖。
`<RelativeLayout ...>
*Here's a toolbar*
<ScrollView ...>
<LinearLayout ...>
<FrameLayout
android:id="@+id/prof_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_vertical_margin">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="@dimen/size_editing_pic"
android:layout_height="@dimen/size_editing_pic"
android:src="@drawable/ic_profilo_utente"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/floating_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:scaleType="center"
android:tint="@color/dark_gray"
app:layout_anchor="@id/profile_image"
app:layout_anchorGravity="bottom|end|right"
app:srcCompat="@drawable/ic_camera"
app:backgroundTint="@color/colorAccentEasy"
app:fabSize="mini"/>
</FrameLayout>
*Other sections*
</LinearLayout>
</ScrollView>
</RelativeLayout>`
FAB是正確的,但是您可以看到它的陰影已被清楚地切掉而沒有所謂的褪色。
FAB不在應有的位置。
我嘗試了所有想到的事情,並在網上找到了這些內容。 我知道,這與操作系統在Lollipop之前或之后的核心代碼執行的不同填充管理以及屬性useCompatPadding
(我嘗試了不同的組合)有關,但是我無法弄清楚如何成功解決它。
多虧了這個建議,我才得以解決問題#1。
至於第二期,我仍然無法解決。 在我的新fabStyle-v21中,由於它非常適合我的需求,所以layout_margins
為5dp。 然后,我嘗試將0dp頁邊距設置為默認樣式,但還是一無所獲,甚至嘗試使用負頁邊距。
問題1-如果要查看FAB, Button
等視圖的elevation
(陰影),則必須在其周圍留出空間。 例如,您可以設置邊距。 如果FAB
和父布局的邊緣之間沒有空格,則對FAB
的父布局使用填充是無效的
問題2-浮動操作按鈕在Lollipop設備之前具有不同的邊距和填充值。 您可以將不同的styles.xml文件用於不同的api級別。
不同文件夾的styles.xml:
值-v21
<style name="fabStyle">
<item name="android:layout_margin">16dp</item>
</style>
值 (默認)
<style name="fabStyle">
<item name="android:layout_marginLeft">0dp</item>
<item name="android:layout_marginTop">0dp</item>
<item name="android:layout_marginRight">8dp</item>
<item name="android:layout_marginBottom">0dp</item>
</style>
用法:
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/fabStyle"
app:useCompatPadding="true"
app:elevation="6dp"/>
更新:嘗試將app:useCompatPadding="true"
到您的FAB中,而不更改其他安排。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.