FrameLayout+LinearLayout实现首页底部菜单

1.布局样式

 FrameLayout+LinearLayout实现首页底部菜单_第1张图片

 

 

 2.main.xml代码




    

    

    

    

        

            
        

        

            

            
        

        

            
        
    

3.activity功能实现

public class MainActivity extends AppCompatActivity {

    MainActivityBinding binding;

    HomeFragment mHomeFragment =  HomeFragment.newInstance();
    NoticeFragment mNoticeFragment = NoticeFragment.newInstance();
    MineFragment mMineFragment = MineFragment.newInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = MainActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.llHomeATMy.setOnClickListener(mClickListener);//为第一个导航图片添加单机事件
        binding.llHomeATHome.setOnClickListener(mClickListener);//为第二个导航图片添加单机事件
        binding.llHomeATNotice.setOnClickListener(mClickListener);//为第三个导航图片添加单机事件

        FragmentManager fm = getSupportFragmentManager();//获取Fragment的管理器
        FragmentTransaction transaction = fm.beginTransaction();// 开启一个事务
        transaction.add(R.id.flHomeFragment, mHomeFragment).commit();//设置初始的fragment
        binding.llHomeATHome.setSelected(true);
    }

    //创建单机事件监听器
    View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm = getSupportFragmentManager();//获取Fragment的管理器
            FragmentTransaction transaction = fm.beginTransaction();// 开启一个事务
            int id = v.getId();//通过获取点击的id判断点击了哪个张图片
            if (id == R.id.llHomeATHome) {
                transaction.replace(R.id.flHomeFragment, mHomeFragment); //替换Fragment
                binding.llHomeATHome.setSelected(true);
                binding.llHomeATNotice.setSelected(false);
                binding.llHomeATMy.setSelected(false);
            } else if (id == R.id.llHomeATNotice) {
                transaction.replace(R.id.flHomeFragment, mNoticeFragment); //替换Fragment
                binding.llHomeATNotice.setSelected(true);
                binding.llHomeATHome.setSelected(false);
                binding.llHomeATMy.setSelected(false);
            } else if (id == R.id.llHomeATMy) {
                transaction.replace(R.id.flHomeFragment, mMineFragment); //替换Fragment
                binding.llHomeATMy.setSelected(true);
                binding.llHomeATNotice.setSelected(false);
                binding.llHomeATHome.setSelected(false);
            }
            transaction.commit(); //提交事务
        }
    };

}

你可能感兴趣的:(android,java,开发语言)