Android 使用FragmentTabhost代替Tabhost

前言:
现在Fragment使用越来越广了,虽然Fragment寄生在Activity下,但是它的出现对于开发者来说是一件非常幸运的事,使开发的效率更高效了,好了下面就说说 FragmentTabhost的使用,因为Tabhost已经不推荐使用了,现在一般都使用FragmentTabhost!我本身也个菜鸟,就是帮帮新手,因为Fragment是3.0才出现,为了避免3.0以下的使用不了,所以我们要用v4包来支持,不要倒错包哦!大神勿喷!
一:首先我们看看XML:
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/realtabcontent" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" /> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/bg_tabhost_bg"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost> </LinearLayout>
2.tab_item_view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:padding="3dp" android:src="@drawable/tab_home_btn"> </ImageView> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textSize="10sp" android:textColor="#ffffff"> </TextView> </LinearLayout>
3.fragment1.xml 就贴一个Fragment XML吧!其他的几个都一样,只是颜色不一样,呵呵!
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#FBB55D" > </LinearLayout>
ok,XML先写完了,那我们看看代码吧!
4.MainActivity
package com.example.fragmenttabhost;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import com.example.fragment.Fragment1;
import com.example.fragment.Fragment2;
import com.example.fragment.Fragment3;
import com.example.fragment.Fragment4;
import com.example.fragment.Fragment5;
/**
*
* @author zqy
*
*/
public class MainActivity extends FragmentActivity {
/**
* FragmentTabhost
*/
private FragmentTabHost mTabHost;
/**
* 布局填充器
*
*/
private LayoutInflater mLayoutInflater;
/**
* Fragment数组界面
*
*/
private Class mFragmentArray[] = { Fragment1.class, Fragment2.class,
Fragment3.class, Fragment4.class, Fragment5.class };
/**
* 存放图片数组
*
*/
private int mImageArray[] = { R.drawable.tab_home_btn,
R.drawable.tab_message_btn, R.drawable.tab_selfinfo_btn,
R.drawable.tab_square_btn, R.drawable.tab_more_btn };
/**
* 选修卡文字
*
*/
private String mTextArray[] = { "首页", "消息", "好友", "搜索", "更多" };
/**
*
*
*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
* 初始化组件
*/
private void initView() {
mLayoutInflater = LayoutInflater.from(this);
// 找到TabHost
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
// 得到fragment的个数
int count = mFragmentArray.length;
for (int i = 0; i < count; i++) {
// 给每个Tab按钮设置图标、文字和内容
TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i])
.setIndicator(getTabItemView(i));
// 将Tab按钮添加进Tab选项卡中
mTabHost.addTab(tabSpec, mFragmentArray[i], null);
// 设置Tab按钮的背景
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.selector_tab_background);
}
}
/**
*
* 给每个Tab按钮设置图标和文字
*/
private View getTabItemView(int index) {
View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
imageView.setImageResource(mImageArray[index]);
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(mTextArray[index]);
return view;
}
}
5.Fragment1.java Fragment其他几个都一样,指不过XML不一样!
package com.example.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.fragmenttabhost.R;
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, null);
}
}
OK 基本上写完了,让我们看看效果!
哈哈,效果还算可以!好了,去吃饭了!
资源下载地址:http://xiazai./201705/yuanma/FragmentTabhost().rar
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
# Android
# 使用FragmentTabhost代替Tabhost
# FragmentTabhost代替Tabhost实现代码
# Android组件TabHost实现页面中多个选项卡切换效果
# android TabHost(选项卡)的使用方法
# android 选项卡(TabHost)如何放置在屏幕的底部
# Android组件必学之TabHost使用方法详解
# Android控件之TabHost用法实例分析
# 详解Android应用中使用TabHost组件进行布局的基本方法
# Android编程实现设置TabHost当中字体的方法
# 详解Android TabHost的多种实现方法 附源码下载
# Android Tabhost使用方法详解
# Android TabHost组件使用方法详解
# Android TabHost选项卡标签图标始终不出现的解决方法
# 几个
# 好了
# 菜鸟
# 让我们
# 下载地址
# 其他的
# 希望能
# 大神
# 要用
# 还算
# 谢谢大家
# 首页
# 为了避免
# 上写
# 选项卡
# 先写
# 使用了
# 是一件
# 倒错
# bg_tabhost_bg
相关文章:
如何在搬瓦工VPS快速搭建网站?
北京的网站制作公司有哪些,哪个视频网站最好?
微信小程序制作网站有哪些,微信小程序需要做网站吗?
沈阳个人网站制作公司,哪个网站能考到沈阳事业编招聘的信息?
建站之星×万网:智能建站系统+自助建站平台一键生成
微信h5制作网站有哪些,免费微信H5页面制作工具?
极客网站有哪些,DoNews、36氪、爱范儿、虎嗅、雷锋网、极客公园这些互联网媒体网站有什么差异?
专业网站设计制作公司,如何制作一个企业网站,建设网站的基本步骤有哪些?
专业商城网站制作公司有哪些,pi商城官网是哪个?
如何制作公司的网站链接,公司想做一个网站,一般需要花多少钱?
如何登录建站主机?访问步骤全解析
全景视频制作网站有哪些,全景图怎么做成网页?
如何在Windows服务器上快速搭建网站?
外贸公司网站制作哪家好,maersk船公司官网?
如何快速辨别茅台真假?关键步骤解析
宠物网站制作html代码,有没有专门介绍宠物如何养的网站啊?
建站之星体验版:智能建站系统+响应式设计,多端适配快速建站
如何确保西部建站助手FTP传输的安全性?
建站主机如何选?高性价比方案全解析
网站插件制作软件免费下载,网页视频怎么下到本地插件?
制作网页的网站有哪些,电脑上怎么做网页?
如何快速搭建高效香港服务器网站?
C#怎么创建控制台应用 C# Console App项目创建方法
如何在IIS中新建站点并配置端口与IP地址?
mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?
哈尔滨网站建设策划,哈尔滨电工证查询网站?
孙琪峥织梦建站教程如何优化数据库安全?
如何快速搭建二级域名独立网站?
ppt在线制作免费网站推荐,有什么下载免费的ppt模板网站?
网站制作的方法有哪些,如何将自己制作的网站发布到网上?
php8.4新语法match怎么用_php8.4match表达式替代switch【方法】
标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?
国美网站制作流程,国美电器蒸汽鍋怎么用官方网站?
C#怎么使用委托和事件 C# delegate与event编程方法
制作网站的模板软件,网站怎么建设?
建站之星CMS五站合一模板配置与SEO优化指南
小米网站链接制作教程,请问miui新增网页链接调用服务有什么用啊?
如何在新浪SAE免费搭建个人博客?
弹幕视频网站制作教程下载,弹幕视频网站是什么意思?
建站主机选哪家性价比最高?
潮流网站制作头像软件下载,适合母子的网名有哪些?
广东专业制作网站有哪些,广东省能源集团有限公司官网?
c++怎么用jemalloc c++替换默认内存分配器【性能】
Python文件管理规范_工程实践说明【指导】
网站制作话术技巧,网站推广做的好怎么话术?
如何在香港免费服务器上快速搭建网站?
建站之星如何一键生成手机站?
C++如何使用std::optional?(处理可选值)
建站主机CVM配置优化、SEO策略与性能提升指南
如何配置WinSCP新建站点的密钥验证步骤?
*请认真填写需求信息,我们会在24小时内与您取得联系。