介绍
Android开发中使用底部菜单栏的频次非常高,主要的实现手段有以下:
- TabWidget
- 隐藏TabWidget,使用RadioGroup和RadioButton
- FragmentTabHost
- 5.0以后的TabLayout
- 最近推出的 Bottom navigation
案例1:简单使用
简单使用 FragmentTabHost
1 在布局文件使用 FragmentTabHost,并提供 Fragment 的容器
2 在 Activity 里查找 FragmentTabHost,并使用 setUp 方法关联到 Fragment 的容器
3 向 FragmentTabHost 里添加 Tab,注意 Tab 需要设置 indicate 文本
4 使用newInstance创建fragment
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.itcast.demo.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f0f">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:src="@drawable/actionbar_search_icon" />
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00"
android:orientation="vertical">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
//特殊之处,高设置0dp weight设置1
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout>
<android.support.v4.app.FragmentTabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#00f"></LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("heihehie");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
drawerLayout.addDrawerListener(toggle);// 面板展开的监听设置给 Toogle
toggle.syncState();// 初始化 toogle 的绘制内容
// 设置底部栏
FragmentTabHost tabHost = (FragmentTabHost) findViewById(R.id.tabhost);
tabHost.setup(this,getSupportFragmentManager(),R.id.container);// 初始化 tabhost
// 添加一个 tab
TabHost.TabSpec tab1 = tabHost.newTabSpec("news");// 创建tab对象
tab1.setIndicator("资讯");// 设置tab 内容
Bundle arg = new Bundle();
arg.putString("content","资讯界面");
tabHost.addTab(tab1,TestFragment.class,arg);// 将tab添加到底部栏
// 再添加一个 Tab
TabHost.TabSpec tab2 = tabHost.newTabSpec("tweet");
tab2.setIndicator("动弹");
Bundle arg2 = new Bundle();
arg2.putString("content","动弹界面");
tabHost.addTab(tab2,TestFragment.class,arg2);
}
@Override
// 为 Activity 生成菜单,ToolBar已经被设置为标题栏,这个菜单会自动显示到 ToolBar 上
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
// 目录菜单的点击响应
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_search:
Toast.makeText(this, "跳转到搜索界面", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}
TestFragment
public class TestFragment extends Fragment {
public static TestFragment newInstance(String content){
Bundle arg = getBundle(content);
TestFragment fragment = new TestFragment();
fragment.setArguments(arg);
return fragment;
}
@NonNull
public