【问题标题】:BottomNavigation not work in fragment with component navigationBottomNavigation 在带有组件导航的片段中不起作用
【发布时间】:2021-10-18 13:23:25
【问题描述】:

底部导航在我单击时不会更改片段,它适用于活动,但当我在片段中使用它时,它不会。

我的片段

public class MessageFragment extends Fragment {

    private FragmentMessageBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentMessageBinding.inflate(inflater,container,false);


        NavController navController = NavHostFragment.findNavController(MessageFragment.this);
        NavigationUI.setupWithNavController(binding.answerNavView22, navController);


        return binding.getRoot();
    }

在片段的 xml 中

<RelativeLayout
        android:backgroundTint="#E3E1E1"
        android:layout_marginTop="2dp"
        android:layout_marginHorizontal="2dp"
        android:visibility="visible"
        android:id="@+id/layout_answer"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:animateLayoutChanges="true"
        android:background="@drawable/corner_top"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar2">

        <androidx.fragment.app.FragmentContainerView
            android:layout_below="@id/answer_nav_view22"
            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:navGraph="@navigation/nav_answer" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:background="#E3E1E1"
            android:id="@+id/answer_nav_view22"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:menu="@menu/menu_answer_nav" />
    </RelativeLayout>

我检查了 id menu ,它与 nav_answer 中的 id 相同。我不知道为什么,请帮帮我!

祝大家有美好的一天!

【问题讨论】:

    标签: java android components


    【解决方案1】:

    Mình thấy như bạn đang làm thì nó sẽ là fragment trong fragment(主机 sẽ là fragment cha và mỗi tab trong bottom nav sẽ là frag con)。导航组件đâu có support dạng này

    【讨论】:

    • 2 cái đó cũng giống 底部导航 đó bạn。 Có điều là thằng 导航组件 không hỗ trợ, cái đó mình phải làm bằng tay như thông thường.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多