【问题标题】:Android Java Bottom navigation bar not visible when using fragments使用片段时Android Java底部导航栏不可见
【发布时间】:2021-11-14 19:57:29
【问题描述】:

我正在尝试实现底部导航视图。导航栏在不使用片段的情况下完美显示,但在实现片段后它不再可见。然而,导航栏仍然是可点击的,并且似乎片段正在其上呈现。我不确定我错过了什么。

没有片段的底部导航:bottom navigation without fragments

带有片段的底部导航:bottom navigation with fragments

Main.java

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();
        }
    });

}

}

activity_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" />

HomeFragment.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}

【问题讨论】:

    标签: java android


    【解决方案1】:

    只需将片段与导航分开,它们不会重叠

    activity_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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多