Android底部导航菜单的实现
1. 效果预览
<?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