4/16学习总结:Fragment

今天主要学习了有关Fragment的知识

明天准备继续完善这一部分代码,使其能够显示信息。

遇到的问题:开始没搞懂逻辑关系,没能将底部按钮与显示内容绑定起来,现已解决

详情参考:https://www.cnblogs.com/sunxun/p/9241180.html

在这里做一下总结:

1.Activity的布局文件分为两部分:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

下面的按钮部分属于ButtonNavigation部分,上面的内容属于Fragment。

ButtonNavigation的布局文件通常写在menu中,即menu中定义了各个按钮的布局

然后添加各个fragment各自的布局文件,在Activity.java中为按钮和界面绑定。

猜你喜欢

转载自www.cnblogs.com/wangzhaojun1670/p/12715066.html