Android 实现底部导航条有多种方式
1BottomNavigationView+Fragment
2RadioGroup+Fragment
3TabHost+Fragment
BottomNavigationView+Fragment 实现
xml文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" > <FrameLayout android:id="@+id/content_main" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottomNavigation" android:layout_marginBottom="6dp" tools:ignore="Suspicious0dp" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:id="@+id/bottomNavigation" app:menu="@menu/navigation" app:itemTextColor="@drawable/select_text_color" app:backgroundTint="@color/white" app:labelVisibilityMode="labeled" /> </RelativeLayout>navigation.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="@string/home" android:icon="@drawable/select_home" android:id="@+id/home" /> <item android:title="@string/find" android:icon="@drawable/select_home" android:id="@+id/find" /> <item android:title="@string/shop" android:icon="@drawable/select_home" android:id="@+id/shop" /> <item android:title="@string/person" android:icon="@drawable/select_home" android:id="@+id/person" /> </menu>selecter
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#FF0000" /> <item android:state_focused="true" android:color="#FF0000" /> <item android:state_checked="true" android:color="#FF0000"/> <item android:color="#0000FF" /> </selector>java 代码
package com.coderxl.ltd.Ui.Activity; import android.os.Bundle; import android.view.MenuItem; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; import com.coderxl.ltd.R; import com.coderxl.ltd.Ui.Fragment.BaseFragment; import com.coderxl.ltd.Ui.Fragment.HomeFragment; import com.coderxl.ltd.Ui.Fragment.SearFragment; import com.google.android.material.bottomnavigation.BottomNavigationView; import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; public class TwoActivity extends AppCompatActivity { @BindView(R.id.bottomNavigation) BottomNavigationView bottomNavigation; private List<BaseFragment> mflist = null; private int Currnt_Postion=0; private Fragment tempFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); ButterKnife.bind(this); bottomNavigation.setItemIconTintList(null); initFragment(); initLisner(); } private void initLisner() { bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()){ case R.id.home: Currnt_Postion=0; break; case R.id.find: Currnt_Postion=1; break; } BaseFragment fragment = getFragment(Currnt_Postion); switchUi(tempFragment,fragment); return true; } }); bottomNavigation.setSelectedItemId(R.id.home); } private void initFragment() { mflist = new ArrayList<>(); mflist.add(new HomeFragment()); mflist.add(new SearFragment()); } private BaseFragment getFragment(int currnt_Postion) { BaseFragment baseFragment = mflist.get(currnt_Postion); return baseFragment; } private void switchUi(Fragment fromFragment, BaseFragment nextfragment) { if(tempFragment!=nextfragment){ tempFragment=nextfragment; if(nextfragment!=null){ FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); if(!nextfragment.isAdded()){ if(fromFragment!=null){ fragmentTransaction.hide(fromFragment); } fragmentTransaction.add(R.id.content_main,nextfragment).commit(); }else{ if(fromFragment!=null){ fragmentTransaction.hide(fromFragment); } fragmentTransaction.show(nextfragment).commit(); } } } } }
RadioGroup+Fragment
<?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/content_main"
android:layout_width="match_parent"
android:layout_height="0dp"
tools:ignore="Suspicious0dp"
android:layout_weight="1"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:id="@+id/rg"
>
<RadioButton
android:id="@+id/home"
android:gravity="center"
android:text="首页"
android:button="@null"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent"
/>
<RadioButton
android:textColor="@drawable/select_text_color"
android:drawableTop="@drawable/select_home"
android:id="@+id/find"
android:layout_marginTop="5dp"
android:drawablePadding="2dp"
android:gravity="center"
android:text="发现"
android:button="@null"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent"
/>
<RadioButton
android:id="@+id/shop"
android:text="商城"
android:button="@null"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center"
/>
<RadioButton
android:text="个人"
android:button="@null"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center"
android:id="@+id/person"
/>
</RadioGroup>
</LinearLayout>
java代码
package com.coderxl.ltd;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.coderxl.ltd.Ui.Fragment.BaseFragment;
import com.coderxl.ltd.Ui.Fragment.HomeFragment;
import com.coderxl.ltd.Ui.Fragment.SearFragment;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.content_main)
FrameLayout contentMain;
@BindView(R.id.rg)
RadioGroup rg;
private List<BaseFragment> mflist = null;
private int Currnt_Postion=0;
private Fragment tempFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initFragment();
initListner();
rg.check(R.id.home);
}
private void initListner() {
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.home:
Currnt_Postion=0;
break;
case R.id.find:
Currnt_Postion=1;
break;
case R.id.shop:
Currnt_Postion=2;
break;
case R.id.person:
Currnt_Postion=3;
break;
};
BaseFragment fragment = getFragment(Currnt_Postion);
switchUi(tempFragment,fragment);
}
});
}
private void switchUi(Fragment fromFragment, BaseFragment nextfragment) {
if(tempFragment!=nextfragment){
tempFragment=nextfragment;
if(nextfragment!=null){
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if(!nextfragment.isAdded()){
if(fromFragment!=null){
fragmentTransaction.hide(fromFragment);
}
fragmentTransaction.add(R.id.content_main,nextfragment).commit();
}else{
if(fromFragment!=null){
fragmentTransaction.hide(fromFragment);
}
fragmentTransaction.show(nextfragment).commit();
}
}
}
}
private BaseFragment getFragment(int currnt_Postion) {
BaseFragment baseFragment = mflist.get(currnt_Postion);
return baseFragment;
}
private void initFragment() {
mflist = new ArrayList<>();
mflist.add(new HomeFragment());
mflist.add(new SearFragment());
}
}
来源:CSDN
作者:一抹离愁
链接:https://blog.csdn.net/xueshao110/article/details/104771079