安卓开发:安卓底部菜单栏的实现,RadioGroup 和Fragment

安卓的底部菜单 有很多实现方式,本文以RadioGroup 作为 底部单选切换。用fragment 作为页面显示隐藏

初次接触安卓开发,有不对的地方大家交流沟通!

首先是布局文件

我们需要一个FrameLayout 放置 Fragment ,以及RadioGroup 放置底部菜单

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFFFF"
        android:orientation="vertical" >
        <FrameLayout
            android:id="@+id/frm_all"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >
        FrameLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="@drawable/fbutton_color_emerald">LinearLayout>
        <RadioGroup
            android:id="@+id/radiogroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="center_vertical"
            android:orientation="horizontal" >
            <RadioButton
                android:id="@+id/radio_main"
                style="@style/main_tab_bottom"
                android:layout_weight="1"
                android:drawableTop="@drawable/footer_main_selector"
                android:text="首 页" />
            <RadioButton
                android:id="@+id/radio_video"
                style="@style/main_tab_bottom"
                android:layout_weight="1"
                android:drawableTop="@drawable/footer_video_selector"
                android:text="视 频" />
            <RadioButton
                android:id="@+id/radio_new"
                style="@style/main_tab_bottom"
                android:layout_weight="1"
                android:drawableTop="@drawable/footer_new_selector"
                android:text="新 闻" />

            <RadioButton
                android:id="@+id/radio_mycenter"
                style="@style/main_tab_bottom"
                android:layout_weight="1"
                android:drawableTop="@drawable/footer_mycenter_selector"
                android:text="我 的" />

        RadioGroup>
    LinearLayout>

RelativeLayout>

布局文件中 引用了 一个Style ,设置一些颜色 字体大小等

    

位于drawable 文件夹下边 定义了一个资源文件 设置了选中 和未选中的 样式图片

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
  <item android:state_focused="true" android:drawable="@drawable/footer_btn_home_preesed" />
  <item android:state_pressed="true" android:drawable="@drawable/footer_btn_home_preesed"  />
  <item android:state_selected="true" android:drawable="@drawable/footer_btn_home_preesed" />
  <item android:state_checked="true"  android:drawable="@drawable/footer_btn_home_preesed">item>
  <item android:drawable="@mipmap/footer_btn_home" />
selector>

然后就是 activity,设置 RadioGroup 的点击事件。点击后隐藏其他 fragment 显示点击的按钮对应的 利用 FragmentManager 管理fragment

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioGroup;



public class MainActivity extends AppCompatActivity {

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;



    MainFragment mainFragment;
    VideoHomeFragment videoFragment;
    MyCentent myCenterFragment;
    ProHomeFragment proHomeFragment;
    SkillHomeFragment skillFragment;

    private RadioGroup radiogroup;
    private int menuid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
       // getFragmentManager().beginTransaction().add(R.id.frm_all,new MainFragment()).commit();
        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        radiogroup = (RadioGroup) findViewById(R.id.radiogroup);


        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                menuid = checkedId;
                switch (checkedId) {
                    case R.id.radio_main:
                        setChoiceItem(0);
                        break;
                    case R.id.radio_mycenter:
                        setChoiceItem(4);
                        break;
                        case R.id.radio_skill:
                            setChoiceItem(2);
                            break;
                    case R.id.radio_pro:
                        setChoiceItem(3);
                        break;
                    case R.id.radio_video:
                        setChoiceItem(1);
                }
            }
        });
        radiogroup.check(R.id.radio_main);
    }

    public void setChoiceItem(int index) {
        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragments(fragmentTransaction);
        switch (index) {
            case 0:
                fragmentTransaction.show(mainFragment);
                break;
            case 1:
                fragmentTransaction.show(videoFragment);
                break;
            case 2:
                fragmentTransaction.show(skillFragment);
                break;
            case 3:
                fragmentTransaction.show(proHomeFragment);
                break;
            case 4:

                fragmentTransaction.show(myCenterFragment);
                break;
        }
        fragmentTransaction.commit();
    }


    //隐藏所有的Fragment,避免fragment混乱
    private void hideFragments(FragmentTransaction transaction) {
        if (mainFragment == null) {
            mainFragment = new MainFragment();
            transaction.add(R.id.frm_all, mainFragment);
        }
        if (videoFragment == null) {
            videoFragment = new VideoHomeFragment();
            transaction.add(R.id.frm_all, videoFragment);
        }
        if (myCenterFragment == null) {
            myCenterFragment = new MyCentent();
            transaction.add(R.id.frm_all, myCenterFragment);
        }
        if (skillFragment == null) {
            skillFragment = new SkillHomeFragment();
            transaction.add(R.id.frm_all, skillFragment);
        }
        if (proHomeFragment == null) {
            proHomeFragment = new ProHomeFragment();
            transaction.add(R.id.frm_all, proHomeFragment);
        }
        transaction.hide(proHomeFragment);
        transaction.hide(mainFragment);
        transaction.hide(videoFragment);
        transaction.hide(skillFragment);
        transaction.hide(myCenterFragment);

    }




}

你可能感兴趣的:(Android)