您的位置:首页 > 移动开发 > Android开发

Android FragmentTabHost+FrameLayout 实现底部菜单

2020-02-17 05:14 253 查看

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

activity_main.xml

[code]<?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

[code]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;

}

}

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

  • 点赞
  • 收藏
  • 分享
  • 文章举报
kEEshX 发布了4 篇原创文章 · 获赞 1 · 访问量 551 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: