利用RadioGroup嵌套RadioButton实现Android主页面底部导航栏动态设置图片大小

之前在实现底部导航栏都是  一堆布局嵌套的  对点击切换事件处理也是很麻烦 需要很大的代码量 而且逻辑也有些乱  
后来就想到了RadioGroup嵌套RadioButton来实现 逻辑比较清晰  在Activity里直接给RadioGrop设置OnCheckedChangeListener 然后通
过点击的View的ID  来判断是哪一个Button所点击 并实现相应的逻辑就可以实现了
值得注意一点  在XMl文件里没有办法设置Button里面的图片的大小的属性  稍微高分辨率的图片就会显示很大或者显示不全  总之样子很丑
那么Android除了静态设置布局还可以在代码动态设置 好了  废话不多说直接上代码
 
  
package com.youdu.activity;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.youdu.R;
import com.youdu.activity.base.BaseActivity;
import com.youdu.view.fragment.home.PondFragment;
import com.youdu.view.fragment.home.HomeFragment;
import com.youdu.view.fragment.home.MessageFragment;
import com.youdu.view.fragment.home.MineFragment;

/**
 * Created by ITBOY on 2017/4/15.
 * @function 主页面以及包含的四个fragment,以及所对应的4个底部导航栏
 */

public class HomeActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    private FragmentManager mFragmentManager;
    private HomeFragment mHomeFragment;
    private PondFragment mPondFragment;
    private MessageFragment mMessageFragment;
    private MineFragment mMineFragment;


    private Fragment mCurrent;//显示缓存 记录上一次显示的fragment 用于隐藏时 调用

    private RadioGroup mRadioGroup;//四个底部导航Button的父容器
    private RadioButton mHomeButton;//首页Button
    private RadioButton mShopButton;//商城Button
    private RadioButton mMessageButton;//
    private RadioButton mMineButton;

    //动态设置图片的大小规格
    int left=1;int top=10;int right=70;int bottom=80;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_layout);
        initData();
        initView();
    }

    private void initData() {
        //初始化底部导航栏控件
        mHomeButton = (RadioButton) findViewById(R.id.home_rbutton);
        mShopButton = (RadioButton) findViewById(R.id.mall_rbutton);
        mMessageButton = (RadioButton) findViewById(R.id.discover_rbutton);
        mMineButton = (RadioButton) findViewById(R.id.mine_rbutton);

        //动态设置底部导航栏的4个Button图片显示大小
        Resources res = this.getResources();

        Drawable drawable01 = res.getDrawable(R.drawable.home_button_selector);
        drawable01.setBounds(left, top, right, bottom);

        Drawable drawable02 = res.getDrawable(R.drawable.mall_button_selector);
        drawable02.setBounds(left, top, right, bottom);

        Drawable drawable03 = res.getDrawable(R.drawable.discover_button_selector);
        drawable03.setBounds(left, top, right, bottom);

        Drawable drawable04 = res.getDrawable(R.drawable.my_button_selector);
        drawable04.setBounds(left, top, right, bottom);

        //设置图片在botton 上显示的位置  分别对应左上右下  我们选择上
        mHomeButton.setCompoundDrawables(null, drawable01, null, null);
        mShopButton.setCompoundDrawables(null, drawable02, null, null);
        mMessageButton.setCompoundDrawables(null, drawable03, null, null);
        mMineButton.setCompoundDrawables(null, drawable04, null, null);
    }

    private void initView() {
        //初始化fragment对象 为了节约程序加载耗时,只初始化一个显示的首页Fragment
        mHomeFragment = new HomeFragment();
        mFragmentManager = getSupportFragmentManager();
        mFragmentManager.beginTransaction().replace(R.id.frame_layout, mHomeFragment).commit();
        mCurrent = mHomeFragment;
        mHomeButton.setChecked(true);
        mRadioGroup = (RadioGroup) findViewById(R.id.home_radio_group);
        //设置监听
        mRadioGroup.setOnCheckedChangeListener(this);
    }
//为底部四个导航栏点击事件做响应  切换对应的Fragment 显示或隐藏
    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();
        switch (checkedId) {
            case R.id.home_rbutton:
                mFragmentTransaction.hide(mCurrent);//隐藏上一次显示的Fragment
                if (mHomeFragment==null) {//
                    mHomeFragment=new HomeFragment();
                    mFragmentTransaction.add(R.id.frame_layout,mHomeFragment);
                }else {
                    mFragmentTransaction.show(mHomeFragment);
                }
                mCurrent = mHomeFragment;
                break;

            case R.id.mall_rbutton:
                mFragmentTransaction.hide(mCurrent);//隐藏上一次显示的Fragment
                if (mPondFragment==null) {
                    mPondFragment=new PondFragment();
                    mFragmentTransaction.add(R.id.frame_layout,mPondFragment);
                }else {
                    mFragmentTransaction.show(mPondFragment);
                }
                mCurrent=mPondFragment;
                break;

            case R.id.discover_rbutton:
                mFragmentTransaction.hide(mCurrent);//隐藏上一次显示的Fragment
                if (mMessageFragment==null) {
                    mMessageFragment=new MessageFragment();
                    mFragmentTransaction.add(R.id.frame_layout,mMessageFragment);
                }else {
                    mFragmentTransaction.show(mMessageFragment);
                }
                mCurrent=mMessageFragment;
                break;

            case R.id.mine_rbutton:
                mFragmentTransaction.hide(mCurrent);//隐藏上一次显示的Fragment
                if (mMineFragment==null) {
                    mMineFragment=new MineFragment();
                    mFragmentTransaction.add(R.id.frame_layout,mMineFragment);
                }else {
                    mFragmentTransaction.show(mMineFragment);
                }
                mCurrent=mMineFragment;
                break;
        }
        mFragmentTransaction.commit();//最后提交Fragment事务

    }
}
下边是运行的效果图 点击不同的按钮 会切换不同的对应的Fragment
 
 

你可能感兴趣的:(UI相关)