Android底部導航菜單的實現


Android底部導航菜單的實現

1. 效果預覽


2. 底部菜單 nav.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/nav_home_item"
        android:icon="@drawable/home_item"
        android:title="@string/home_title" />
    <item
        android:id="@+id/nav_message_item"
        android:icon="@drawable/message_item"
        android:title="@string/message" />
    <item
        android:id="@+id/nav_my_item"
        android:icon="@drawable/my_item"
        android:title="@string/my" />

</menu>

3. Fragmengt嵌入頁面

3.1 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=".HomeFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />
</FrameLayout>
3.2 HomeFragment.java
package com.example.myshop;

import android.os.Bundle;

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

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class HomeFragment extends Fragment {

    String text;

    public HomeFragment(String text) {
        // Required empty public constructor
        this.text=text;
    }

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView te=view.findViewById(R.id.text);
        te.setText(text);

    }
}

4.主Activaty

4.1 布局 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/connect"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp">

    </LinearLayout>
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bnv_menu"
        android:layout_width="match_parent"
        android:layout_gravity="bottom"
        android:layout_height="wrap_content"
        app:menu="@menu/nav"/>

</LinearLayout>
4.2 MainActivity.java
package com.example.myshop;

import android.os.Bundle;
import android.view.MenuItem;

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

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bnv_menu;
    private Fragment home;
    private Fragment message;
    private Fragment my;

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

    private void initBottomNav() {
        home=new HomeFragment("主頁");
        message=new HomeFragment("消息");
        my=new HomeFragment("我的");
        bnv_menu.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.nav_home_item:
                        getSupportFragmentManager().beginTransaction().replace(R.id.connect,home).commitNow();
                        break;
                    case R.id.nav_message_item:
                        getSupportFragmentManager().beginTransaction().replace(R.id.connect,message).commitNow();
                        break;
                    case R.id.nav_my_item:
                        getSupportFragmentManager().beginTransaction().replace(R.id.connect,my).commitNow();
                        break;
                }
                return true;
            }
        });
    }

    private void initView() {
        bnv_menu = (BottomNavigationView) findViewById(R.id.bnv_menu);
    }
}

5.string

<resources>
    <string name="app_name">MyShop</string>
    <string name="home_title">主頁</string>
    <string name="message">消息</string>
    <string name="my">我的</string>
    <!-- TODO: Remove or change this placeholder text -->
    <string name="hello_blank_fragment">Hello blank fragment</string>
</resources>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM