Android使用RadioGroup实现底部导航栏

(一)实现步骤:

1.界面布局(activity_main)




    

    
        
        
    

2.点击改变图标



    
    

3.点击改变文字颜色



    
    

4.内嵌fragment布局(根据需要创建不同的fragment)




    

5.fragment的activity

public class HomeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_home, container, false);
        return view;
    }
}

6.装载fragment界面的activity

public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {

    private FragmentManager manager;
    private FragmentTransaction transaction;
    private HomeFragment frHome;
    private MyFragment frMy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FrameLayout frameLayout = findViewById(R.id.frame_layout);
        RadioGroup tabBar = findViewById(R.id.tab_bar);
        RadioButton tabHome = findViewById(R.id.tab_home);
        RadioButton tabMy = findViewById(R.id.tab_my);

        tabHome = (RadioButton) tabBar.getChildAt(0);
        tabHome.setChecked(true);
        tabBar.setOnCheckedChangeListener((RadioGroup.OnCheckedChangeListener) this);

        initFrament();
    }

    private void initFrament() {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        frHome = new HomeFragment();
        transaction.add(R.id.fram_layout,frHome);
        transaction.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
            case R.id.tab_home:
                FragmentTransaction ft1 = manager.beginTransaction();
                hideAll(ft1);
                if (frHome != null) {
                    ft1.show(frHome);
                } else {
                    frHome = new HomeFragment();
                    ft1.add(R.id.fram_layout,frHome);
                }
                ft1.commit();
                break;
            case R.id.tab_my:
                FragmentTransaction ft2 = manager.beginTransaction();
                hideAll(ft2);
                if (frMy != null) {
                    ft2.show(frMy);
                } else {
                    frMy = new MyFragment();
                    ft2.add(R.id.fram_layout,frMy);
                }
                ft2.commit();
                break;
        }
    }

    private void hideAll(FragmentTransaction ft){
        if (ft==null){
            return;
        }
        if (frHome!=null){
            ft.hide(frHome);
        }
        if (frMy!=null){
            ft.hide(frMy);
        }
    }
}

注:此导航栏只能点击不能左右滑动

(二)效果图

Android使用RadioGroup实现底部导航栏_第1张图片

你可能感兴趣的:(android底部导航栏,android,android,studio)