权限
implementation 'com.google.android.material:material:1.0.0'
1.新建一个叫menu的文件包
2.在menu中添加bottom_nav_menu.xml布局
<item
android:id="@+id/navigation_home"
android:icon="@android:drawable/ic_menu_camera"
android:title="Home" />
<item
android:id="@+id/navigation_dashboard"
android:icon="@android:drawable/ic_dialog_email"
android:title="email" />
<item
android:id="@+id/navigation_notifications"
android:icon="@android:drawable/ic_dialog_map"
android:title="sss" />
在activity_main.xml布局文件中加入
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_nav_menu"/>
mainActivity.java文件夹
package com.example.fu1118;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView nav;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nav=findViewById(R.id.nav);
nav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId() ){
case R.id.navigation_home:
webView.loadUrl("file:///android_asset/new.html");
case R.id.navigation_dashboard:
webView.loadUrl("https://www.baidu.com");
}
return true;
}
});
}
//添加关联菜单的部分
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.bottom_nav_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
webView.loadUrl("file:///android_asset/new.html");
break;
case R.id.navigation_dashboard:
webView.loadUrl("https://www.baidu.com");
break;
}
return true;
}
}