原代碼爲:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:layout_below="@+id/bottomNavigationView"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bottomNavigationView"
app:itemBackground="@color/gray"
app:itemIconTint="@drawable/main_bottom"
app:itemTextColor="@drawable/main_bottom"
app:menu="@menu/menu_bottom_navigation_view"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
此時viewpager裏面的fragment生命週期代碼都走完了,但頁面始終不顯示出來,後面發現是佈局問題,英文單詞搞錯了,below和above搞反了,此時修改爲如下,即可顯示:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:layout_above="@+id/bottomNavigationView"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bottomNavigationView"
app:itemBackground="@color/gray"
app:itemIconTint="@drawable/main_bottom"
app:itemTextColor="@drawable/main_bottom"
app:menu="@menu/menu_bottom_navigation_view"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>