Android FragmentTabHost+FrameLayout 实现底部菜单

本文介绍了如何结合Android的FragmentTabHost和FrameLayout组件来实现底部导航菜单的功能。通过示例代码activity_main.xml和MainActivity.java,展示了具体的实现过程,作者在学习过程中记录了这一实践,并期望探索更多实现方式。

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

项目运行截图(那个阅读,我丢了一个资源文件。。。)

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <android.support.v4.app.FragmentTabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="#DA8D8888">

        <FrameLayout
            android:id="@+id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

MainActivity.java

package com.wuyuxi.news;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

import com.wuyuxi.news.Fragment.TabFirstFragment;
import com.wuyuxi.news.Fragment.TabFivethFragment;
import com.wuyuxi.news.Fragment.TabFourthFragment;
import com.wuyuxi.news.Fragment.TabSecondFragment;
import com.wuyuxi.news.Fragment.TabThridFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {
    private static final String Tag = "tabFragmentActivity";
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bundle bundle = new Bundle();
        bundle.putString("tag", Tag);
        mTabHost = findViewById(R.id.tabhost);
// 把TabHost up到 Framlayout 上 id为realtabcontent
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mTabHost.getTabWidget().setDividerDrawable(android.R.color.transparent);


// 添加tab 这个是每一页点击显示的
        mTabHost.addTab(getTabView(R.string.menu_first, R.drawable.tab_first),
                TabFirstFragment.class, bundle);
        mTabHost.addTab(getTabView(R.string.menu_second, R.drawable.tab_second),
                TabSecondFragment.class, bundle);
        mTabHost.addTab(getTabView(R.string.menu_thrid, R.drawable.tab_thrid),
                TabThridFragment.class, bundle);
        mTabHost.addTab(getTabView(R.string.menu_fourth, R.drawable.tab_fourth),
                TabFourthFragment.class, bundle);
        mTabHost.addTab(getTabView(R.string.menu_fiveth, R.drawable.tab_fiveth),
                TabFivethFragment.class, bundle);


    }

    // 获取底边文件
    private TabHost.TabSpec getTabView(int textId, int imgId) {
        // 获取资源文件
        String text = getResources().getString(textId);
        Drawable drawable = getResources().getDrawable(imgId);

        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());

        View item_tabbar = getLayoutInflater().inflate(R.layout.item_tabbar, null);
        TextView tv_item = item_tabbar.findViewById(R.id.tv_item_tabbar);
        tv_item.setText(text);
        tv_item.setTextColor(Color.YELLOW);
        tv_item.setCompoundDrawables(null, drawable, null, null);
        TabHost.TabSpec spec = mTabHost.newTabSpec(text).setIndicator(item_tabbar);
        return spec;

    }




}

学习中记录,希望以后可以用更多的方法实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值