我在Fragment中定位FAB時遇到了問題。FAB在左上角,但我需要它在右下角。
<?xml version="1.0" encoding="utf-8"? >
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"?
xmlns:tools="http://schemas.android.com/tools"。
xmlns:android="http://schemas.android.com/apk/res/android"/span>
android:layout_width="match_parent"/span>
android:layout_height="match_parent"/span>
tools:context=".ui.vrhcaby.VrhcabyFragment"/span>>
<ImageView
android:layout_width="match_parent"。
android:layout_height="match_parent"/span>
android:textAlignment="center"/span>
android:src="@mipmap/dice_180"。
android:layout_margin="100px" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@ id/fab"。
android:layout_width="wrap_content"/span>
android:layout_height="wrap_content"/span>
android:layout_gravity="bottom|end"/span>
android:layout_margin="@dimen/fab_margin"。
app:srcCompat="@android:drawable/ic_dialog_email" />
</RelativeLayout>
我有什么問題?
我有什么問題?
uj5u.com熱心網友回復:
正如我所讀到的,FAB不能很好地使用相對布局,你應該使用協調員布局來代替。
- 首先在build.gradle(Module:app)中包含其依賴性
dependencies {
...
實作 'androidx.coordinatorlayout:coordinatorlayout:1.1.0'/span>
...
}
- 然后像這樣宣告Coordinator Layout而不是相對布局 。
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:app="http://schemas.android.com/apk/res-auto"/span>?
xmlns:tools="http://schemas.android.com/tools"。
xmlns:android="http://schemas.android.com/apk/res/android"/span>
android:layout_width="match_parent"/span>
android:layout_height="match_parent"/span>
tools:context=".ui.vrhcaby.VrhcabyFragment"/span>>
<ImageView
android:layout_width="match_parent"。
android:layout_height="match_parent"/span>
android:textAlignment="center"/span>
android:src="@mipmap/dice_180"。
android:layout_margin="100px" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@ id/fab"。
android:layout_width="wrap_content"/span>
android:layout_height="wrap_content"/span>
android:layout_gravity="bottom|end"/span>
android:layout_margin="@dimen/fab_margin"。
app:srcCompat="@android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
如果你想了解更多資訊,你可以閱讀更多關于FAB的資訊
<uj5u.com熱心網友回復:
相對布局不支持fab,而協調者布局支持。你可以這樣做。
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"/span>
android:layout_width="match_parent"/span>
android:layout_height="match_parent"/span>
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@ id/ivPic"/span>
android:layout_width="match_parent"/span>
android:layout_height="match_parent"/span>
android:src="@drawable/badimalika"/span>
android:scaleType="centerCrop"/span>
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@ id/fab"。
android:layout_width="wrap_content"/span>
android:layout_height="wrap_content"。
app:srcCompat="@android:drawable/ic_dialog_email"。
app:layout_anchorGravity="bottom|end"。
app:layout_anchor="@id/ivPic"/span>
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/321613.html
標籤:

