编译版本在21及以上
第一步、创建三个Fragment 自己自定义命名
HomeFragment
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.ms.lemonnote.R;
public class HomeFragment extends Fragment {
private static final String TAG = "HomeFragment";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_fragment, null);
}
}
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35sp"
android:layout_centerInParent="true"
android:text="HOME"/>
</RelativeLayout>
FindFragMent、 MineFragment 和HomeFragment 一样
第二、在res资源文件下,创建menu资源文件并在其目录下面创建bottom_menu.xml,注意个数不能超过5个否则会报错
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:title="首页" />
<item
android:id="@+id/navigation_find"
android:title="发现" />
<item
android:id="@+id/navigation_mine"
android:title="我的" />
</menu>
第三、创建MainActivity
... 省略引用
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView = null;
private List<Fragment> mFragments;
private Fragment mCurrFragment;
private HomeFragment homeFragment;
private MineFragment mineFragment;
private FindFragment findFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.navigation);
// ===============底部菜单 第一种 BottomNavigationView+Fragment start===================================
//初始化Fragment及底部导航栏
initFragments(savedInstanceState);
bottomNavigationView.setOnNavigationItemSelectedListener(item -> onTabItemSelected(item.getItemId()));
// ===============底部菜单 第一种 BottomNavigationView+Fragment end===================================
}
private void switchFragment(Fragment targetFragment) {
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
if (!targetFragment.isAdded()) {
//第一次使用switchFragment()时currentFragment为null,所以要判断一下
if (mCurrFragment != null) {
transaction.hide(mCurrFragment);
}
transaction.add(R.id.fragment_container, targetFragment, targetFragment.getClass().getName());
} else {
transaction
.hide(mCurrFragment)
.show(targetFragment);
}
mCurrFragment = targetFragment;
transaction.commit();
}
private boolean onTabItemSelected(int id) {
Fragment fragment = null;
switch (id) {
case R.id.navigation_home:
fragment = mFragments.get(0);
break;
case R.id.navigation_find:
fragment = mFragments.get(1);
break;
case R.id.navigation_mine:
fragment = mFragments.get(2);
break;
default:
break;
}
if(fragment == null) {
return false;
}
switchFragment(fragment);
return true;
}
private void initFragments(Bundle savedInstanceState) {
if (savedInstanceState == null) {
homeFragment = new HomeFragment();
findFragment = new FindFragment();
mineFragment = new MineFragment();
mFragments = new ArrayList<>(3);
mFragments.add(homeFragment);
mFragments.add(findFragment);
mFragments.add(mineFragment);
mCurrFragment = homeFragment;
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, homeFragment)
.commit();
}
}
}
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#fcfcfc"
app:itemIconTint="@color/colorPrimaryDark"
app:itemTextColor="@color/colorAccent"
app:menu="@menu/bottom_menu"
app:labelVisibilityMode="labeled"/>
</LinearLayout>
网友评论