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; } }
学习中记录,希望以后可以用更多的方法实现。
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- android.app.Fragment和android.support.v4.app.Fragment使用区别
- Android textview 在drawable中定义xml文件只要边框
- Android中轮播图的实现 .
- Android.mk的基本语法
- android基础概况
- android服务
- android录音和播放视频
- android广播机制
- Android 支付宝接入流程
- Android EditText
- Android Material Design 之TextInputLayout TextInputEditText
- Android Material Design 之TabLayout
- Android MaterialDesign 之 FloatingActionButton
- Android Material Design 之 DrawLayout
- Android studio抓取app应用日志
- Android 夜间模式的实现
- Android中会引起ANR的情况
- Android 底部地址选择弹框
- vim调试android项目脚本完成
- android 两端对齐