Android开发中底部导航栏的简单实现(两种方法)

简单的两种实现方法:

1、利用LinearLayout+TextView实现底部导航栏的效果。

2、利用RadioGroup+RadioButton实现底部导航栏的效果。

两种的效果图一样,如下所示:

Android开发中底部导航栏的简单实现(两种方法)_第1张图片

Android开发中底部导航栏的简单实现(两种方法)_第2张图片

两者的功能代码,基本一致,唯一的区别,也就是:TextView和RadioButton的区别。选择样式中的state_selected和state_checked的区别。

下面附上RadioGroup+RadioButton实现的功能代码:

1、首先是点击效果的实现:

tab_menu_channel.xml



    
    

其他三个,照着写。

tab_menu_bg.xml



    
        
            
        
    
    
        
            
        
    

2、activity_main.xml布局代码如下:




    

        


        

    

    

    

        

        

        

        

    

    

    

styles.xml的代码如下:



    
    

    


MainActivity.java的代码如下:

package com.deepreality.fragmentcaseonedemo;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    //UI Object
    private RadioButton rbtnChannel, rbtnMessage, rbtnMy, rbtnMore;
    private RadioGroup rgroupTabMenu;

    //Fragment Object
    private MyFragmentOne fg1;
    private MyFragmentTwo fg2;
    private MyFragmentThree fg3;
    private MyFragmentFour fg4;
    private FragmentManager fManager;

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

        fManager = getFragmentManager();
        bindViews();

        rbtnChannel.setChecked(true);
    }

    //UI组件初始化与事件绑定
    private void bindViews() {
        rgroupTabMenu = findViewById(R.id.main_rgroupTabMenu);
        rbtnChannel = findViewById(R.id.main_rbtnChannel);
        rbtnMessage = findViewById(R.id.main_rbtnMessage);
        rbtnMore = findViewById(R.id.main_rbtnMore);
        rbtnMy = findViewById(R.id.main_rbtnMy);

        rgroupTabMenu.setOnCheckedChangeListener(this);
    }

    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(fg1 != null)fragmentTransaction.hide(fg1);
        if(fg2 != null)fragmentTransaction.hide(fg2);
        if(fg3 != null)fragmentTransaction.hide(fg3);
        if(fg4 != null)fragmentTransaction.hide(fg4);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);
        switch (checkedId){
            case R.id.main_rbtnChannel:
                if(fg1 == null){
                    fg1 = new MyFragmentOne();
                    fTransaction.add(R.id.ly_content,fg1);
                }else{
                    fTransaction.show(fg1);
                }
                break;
            case R.id.main_rbtnMessage:
                if(fg2 == null){
                    fg2 = new MyFragmentTwo();
                    fTransaction.add(R.id.ly_content,fg2);
                }else{
                    fTransaction.show(fg2);
                }
                break;
            case R.id.main_rbtnMore:
                if(fg3 == null){
                    fg3 = new MyFragmentThree();
                    fTransaction.add(R.id.ly_content,fg3);
                }else{
                    fTransaction.show(fg3);
                }
                break;
            case R.id.main_rbtnMy:
                if(fg4 == null){
                    fg4 = new MyFragmentFour();
                    fTransaction.add(R.id.ly_content,fg4);
                }else{
                    fTransaction.show(fg4);
                }
                break;
        }
        fTransaction.commit();
    }
}

自定义Fragment类MyFragmentOne.java的代码如下:

package com.deepreality.fragmentcaseonedemo;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragmentOne extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_content_one, container, false);
        return view;
    }
}

你可能感兴趣的:(Android开发小功能)