Android底部导航栏—FragmentTabHost+Fragment

本文详细介绍了如何在Android应用中使用FragmentTabHost实现底部导航栏,包括简单使用、模仿新浪微博底部栏和顶部栏导航等多个案例,涵盖了从布局文件到Activity的完整实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

介绍

这里写图片描述
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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值