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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)