底部导航栏是常用的一个工具,大多数的APP都带有底部导航栏,底部导航栏可以方便用户一只手操作,切占用内存比常规的Activity少,底部导航栏使用Fragment+RadioGroup方法来实现,示意图如下:
1.准备8张导航栏的切换图(4张未选中状态,4张选中状态)
ID:radio_msg_0,radio_friend_0,radio_look_0,radio_my_0
radio_msg_1,radio_friend_1,radio_look_1,radio_my_1
2.设置ActivityLayout.xml
(1)放置1个RadioGroup
ID:radiogroup
(2)放置4个RadioButton(根据自己的需要放置RadioButton,一般不超过5个)
ID:btn_0,btn_1,btn_2,btn_3
(3)放置4个Fragment(RadioButton对应的数量)
ID:fragment_msg,fragment_friend,fragment_look,fragment_my
3.设置每个Radio的xml布局
XML ID:radio_msg,radio_friend,radio_look,radio_my
4.设置每个Fragment的xml布局
XML ID:fragment_msg,fragment_friend,fragment_friend,fragment_friend
5.建立继承Fragment类 新建一个package用来存放4个Fragment类
ID:MsgFragment,FriendFragment,LookFragment,MyFragment
package com.example.MyFragment;
import com.example.mytest.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MsgFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//mxl文件
return inflater.inflate(R.drawable.fragment_msg, container, false);
}
}
package com.example.MyFragment;
import com.example.mytest.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FriendFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//mxl文件
return inflater.inflate(R.drawable.fragment_friend, container, false);
}
}
package com.example.MyFragment;
import com.example.mytest.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LookFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//mxl文件
return inflater.inflate(R.drawable.fragment_look, container, false);
}
}
package com.example.MyFragment;
import com.example.mytest.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//mxl文件
return inflater.inflate(R.drawable.fragment_my, container, false);
}
}
6.在Activity里设置Activity
package com.example.mytest;
import com.example.MyFragment.FriendFragment;
import com.example.MyFragment.MsgFragment;
import com.example.MyFragment.MyFragment;
import com.example.mytest.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends Activity
{
private MsgFragment fragment1;
private FriendFragment fragment2;
private MyFragment fragment3;
private RadioGroup radioGroup;
private RadioButton radioButton;
//private FragmentManager manager;
//private FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState)
{
//全屏显示,显示时间和电量
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//开启事务管理,主要处理Fragment
//manager = getFragmentManager();
// transaction = manager.beginTransaction();
//设置切换Fragment
radioGroup = (RadioGroup)findViewById(R.id.radiogroup);
RadioGroupList radigGroupList = new RadioGroupList();
radioGroup.setOnCheckedChangeListener(radigGroupList);
//设置默认按钮为选中状态
radioButton =(RadioButton) findViewById(R.id.btn_0);
radioButton.setChecked(true);
//开始处理Fragment
fragment1 = new MsgFragment();
fragment2 = new FriendFragment();
fragment3 = new MyFragment();
findViewById(R.id.fragment_msg).setVisibility(View.INVISIBLE);
findViewById(R.id.fragment_friend).setVisibility(View.INVISIBLE);
findViewById(R.id.fragment_look).setVisibility(View.INVISIBLE);
findViewById(R.id.fragment_my).setVisibility(View.INVISIBLE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
public class RadioGroupList implements RadioGroup.OnCheckedChangeListener
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
if(group.getId() == R.id.radiogroup)
{
switch (checkedId)
{
case R.id.btn_0:
findViewById(R.id.fragment_msg).setVisibility(View.VISIBLE);
findViewById(R.id.fragment_friend).setVisibility(View.INVISIBLE);
findViewById(R.id.fragment_look).setVisibility(View.INVISIBLE);
findViewById(R.id.fragment_my).setVisibility(View.INVISIBLE);
Log.d("消息", "提示");
break;
case R.id.btn_1:
findViewById(R.id.fragment_msg).setVisibility(View.INVISIBLE);
findViewById(R.id.fragment_friend).setVisibility(View.VISIBLE );
findViewById(R.id.fragment_look).setVisibility(View.INVISIBLE);
findViewById(R.id.fragment_my).setVisibility(View.INVISIBLE);
Log.d("好友", "提示");
break;
case R.id.btn_2:
findViewById(R.id.fragment_msg).setVisibility(View.INVISIBLE);
findViewById(R.id.fragment_friend).setVisibility(View.INVISIBLE);
findViewById(R.id.fragment_look).setVisibility(View.VISIBLE);
findViewById(R.id.fragment_my).setVisibility(View.INVISIBLE );
Log.d("看点", "提示");
break;
case R.id.btn_3:
findViewById(R.id.fragment_msg).setVisibility(View.INVISIBLE);
findViewById(R.id.fragment_friend).setVisibility(View.INVISIBLE);
findViewById(R.id.fragment_look).setVisibility(View.INVISIBLE);
findViewById(R.id.fragment_my).setVisibility(View.VISIBLE );
Log.d("我的", "提示");
break;
default :
break;
}
}
}
}
}
这就是最简单的底部导航栏实现方法,原理非常简单,只要学过一周Android开发的朋友都可以使用,但是这个程序有一个特点是不能实现滑动,ViewPage的实现方法,可以到我的博客看下。
Demo下载地址:https://download.csdn.net/download/nonecode/10625735