Activity 布局有点复杂...它是一个 fragment Activity,底部 fragment ('bottomBarFragmentPlaceholder')是 FAB 放在中间的 bottomAppBar。
Activity 布局如下:
<?xml version="1.0" encoding="utf-8"?><!-- practice_activity.xml -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@id/progressBarTimeText"
android:layout_alignBottom="@id/progressBarTimeText"
android:scaleY="6" />
<TextView
android:id="@+id/progressBarTimeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#00000000"
android:gravity="center" />
</RelativeLayout>
<androidx.viewpager.widget.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/bottomBarFragmentPlaceholder"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
那么BottomAppBar布局如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottomBarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/primaryLightColor"
android:backgroundTint="@color/primaryLightColor"
app:fabAlignmentMode="center"
app:menu="@drawable/ic_action_overflow"
app:navigationIcon="@drawable/ic_menu_24"
app:popupTheme="@style/ThemeOverlay.MaterialComponents.Dark"
app:theme="@style/ThemeOverlay.MaterialComponents.ActionBar" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/baseline_publish_black_24dp"
app:layout_anchor="@id/bar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
为什么我的 FAB 被推下而不是放入支架中?
非常感谢任何帮助。
谢谢, 国标。
最佳答案
好吧,尝试不同的布局,直到我终于把它全部设置好......
关键见解:
在上层不需要允许覆盖功能( float 按钮)所需的 FrameLayout,但在可能的最低层只需要...(请参阅下面更新的布局)
bottomAppBar 布局不能与 layout_height="wrap_content",它需要被赋予占用父级所有空间的能力...("match_parent"),这就是 FAB 将被赋予足够高度的方式正确放置在 Gradle 中。
最后,这里是更新后的布局:
Fragment Activity布局如下:
<?xml version="1.0" encoding="utf-8"?><!-- practice_activity.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@id/progressBarTimeText"
android:layout_alignBottom="@id/progressBarTimeText"
android:scaleY="6" />
<TextView
android:id="@+id/progressBarTimeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#00000000"
android:gravity="center" />
</RelativeLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.viewpager.widget.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_gravity="center|fill_vertical"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/bottomBarFragmentPlaceholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"/>
</FrameLayout>
</LinearLayout>
BottomAppBar布局如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottomBarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/primaryLightColor"
android:backgroundTint="@color/primaryLightColor"
app:fabAlignmentMode="center"
app:menu="@drawable/ic_action_overflow"
app:navigationIcon="@drawable/ic_menu_24"
app:popupTheme="@style/ThemeOverlay.MaterialComponents.Dark"
app:theme="@style/ThemeOverlay.MaterialComponents.ActionBar" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_publish_black_24dp"
app:layout_anchor="@id/bar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我希望这可以帮助将来遇到类似问题的其他人...
快乐的编码... 国标。
关于android - FAB 不 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54035953/