簡體   English   中英

使用片段時 Android Java 底部導航欄不可見

[英]Android Java Bottom navigation bar not visible when using fragments

我正在嘗試實現底部導航視圖。 導航欄在不使用片段的情況下完美顯示,但在實現片段后它不再可見。 然而,導航欄仍然是可點擊的,而且似乎片段正在渲染它。 我不確定我錯過了什么。

無碎片的底部導航:無碎片的底部導航

帶片段的底部導航:帶片段的底部導航

主程序

package com.example.rentalz.User;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;

import com.example.rentalz.R;
import com.ismaeldivita.chipnavigation.ChipNavigationBar;

public class Main extends AppCompatActivity {

ChipNavigationBar chipNavigationBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    chipNavigationBar = findViewById(R.id.bottom_nav_menu);
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
    bottomMenu();

}

private void bottomMenu() {

    chipNavigationBar.setOnItemSelectedListener(new ChipNavigationBar.OnItemSelectedListener() {
        @Override
        public void onItemSelected(int i) {
            Fragment fragment = null;
            switch (i) {
                case R.id.home:
                    fragment = new HomeFragment();
                    break;
                case R.id.search:
                    fragment = new SearchFragment();
                    break;
                case R.id.favourites:
                    fragment = new FavouritesFragment();
                    break;
                case R.id.profile:
                    fragment = new ProfileFragment();
                    break;
            }
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
        }
    });

}

}

活動_main.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".User.Main"
    android:background="#eeeeee"
    android:id="@+id/fragment_container">

    <com.ismaeldivita.chipnavigation.ChipNavigationBar
        android:id="@+id/bottom_nav_menu"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="@color/white"
        app:cnb_unselectedColor="@color/black"
        app:cnb_radius="8dp"
        app:cnb_menuResource="@menu/bottom_navigation_menu"/>

</RelativeLayout>

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".User.HomeFragment"
    android:background="#3498db">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Home Fragment" />

主頁片段.xml

package com.example.rentalz.User;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.rentalz.R;

public class HomeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
         // Inflate the layout for this fragment
         return inflater.inflate(R.layout.fragment_home, container, false);
    }
 v}

只需將片段與導航分開,它們就不會重疊

活動_main.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eeeeee">
    
    <FrameLayout
        android:layout_width="match_parent"
        android:id="@+id/fragment_container"
        android:layout_above="@+id/bottom_nav_menu"
        android:layout_height="match_parent"/>

    <com.ismaeldivita.chipnavigation.ChipNavigationBar
        android:id="@+id/bottom_nav_menu"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="@color/white"
        app:cnb_unselectedColor="@color/black"
        app:cnb_radius="8dp"
        app:cnb_menuResource="@menu/bottom_navigation_menu"/>

</RelativeLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM