Android底部导航菜单的实现

世上唯一不能复制的是时间,唯一不能重演的是人生,唯一不劳而获的是年龄。该怎么走,过什么样的生活,全凭自己的选择和努力。人生很贵,请别浪费!与智者为伍,与良善者同行。Android底部导航菜单的实现,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

Android底部导航菜单的实现

1. 效果预览

Android底部导航菜单的实现
Android底部导航菜单的实现

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>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/217505.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!