需要一些帮助的底部菜单

时间:2019-06-12 14:45:35

标签: java android

大家好,我正在看教程,不确定自己是否做对了!

所以我有一个像这张照片的应用程序

侧边栏

**SideBar**

我想为我的事件片段添加一个底部菜单

底部菜单 **Bottom menu**

所以我到目前为止所做的

创建文件夹

app> res>菜单

bottom_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/bot_home"
        android:icon="@drawable/ic_home"
        android:title="Home" />

    <item
        android:id="@+id/bot_favorites"
        android:icon="@drawable/ic_favorite"
        android:title="Favorites" />

    <item
        android:id="@+id/bot_search"
        android:icon="@drawable/ic_search"
        android:title="Search" />
</menu>

然后我已经创建了所有这些片段(如果我做对了,就不确定)

app> res>菜单

布局

我创建fragment_home.xml,fragment_favorites.xml和fragment_search.xml

**Layout**

它们里面都有这段代码,区别只是android:text和android:background

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_light">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Home"
        android:textSize="30sp"
        android:layout_centerInParent="true"/>

</RelativeLayout>

我为每个人创建一个Java类 app> java> com.example.readytogo

app>java>com.example.readytogo

所以我创建的所有这3个Java类都具有此代码,唯一的变化是fragment_home,fragment_favorites和fragment_search

HomeFragment.java

package com.example.readytogo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class HomeFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       return inflater.inflate(R.layout.fragment_home,container,false);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_navigation"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_navigation"
        android:background="?android:attr/windowBackground"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:id="@+id/toolbar"
            android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:elevation="4dp"/>

        <androidx.fragment.app.FragmentTabHost
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>


    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:id="@+id/nav_view"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu"/>

</androidx.drawerlayout.widget.DrawerLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;

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

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView =findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    if(savedInstanceState == null){
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
            new ProfileFragment()).commit();
    navigationView.setCheckedItem(R.id.nav_profile);

        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);
    }

}

private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                Fragment seletedFragment = null;

                switch (menuItem.getItemId()) {
                    case R.id.bot_home:
                        seletedFragment = new HomeFragment();
                        break;
                    case R.id.bot_favorites:
                        seletedFragment = new FavoritesFragment();
                        break;
                    case R.id.bot_search:
                        seletedFragment = new SearchFragment();
                        break;
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        seletedFragment).commit();

                return true;
            }
        };

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    switch (menuItem.getItemId()){
        case R.id.nav_profile:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new ProfileFragment()).commit();
            break;
        case R.id.nav_events:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new EventsFragment()).commit();
            break;
        case R.id.nav_aboutus:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new AboutUsFragment()).commit();
            break;
        case R.id.nav_faq:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new FAQFragment()).commit();
            break;
        case R.id.nav_settings:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new SettingsFragment()).commit();
            break;
        case R.id.nav_share:
            Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
            break;
        case R.id.nav_logout:
            Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show();
            break;
        }
        drawer.closeDrawer(GravityCompat.START);
    return true;
    }



@Override
public void onBackPressed(){
    if (drawer.isDrawerOpen(GravityCompat.START)){
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

}

当我运行该应用程序时,我只看到了我的侧边栏,并且只得到了它,当我单击家庭收藏夹并进行搜索时什么也没发生

enter image description here

我是android新手,

非常感谢您的帮助!谢谢

0 个答案:

没有答案