Android 使用FragmentTabhost代替Tabhost

 更新时间:2017年05月03日 11:14:01   投稿:lqh  
这篇文章主要介绍了Android 使用FragmentTabhost代替Tabhost的相关资料,需要的朋友可以参考下

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.jb51.net/201705/yuanma/FragmentTabhost(jb51.net).rar

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • Android画中画窗口开启方法

    Android画中画窗口开启方法

    Android8.0 Oreo(API Level26)允许活动启动画中画Picture-in-picture(PIP)模式。PIP是一种特殊类型的多窗口模式,主要用于视频播放。PIP模式已经可用于Android TV,而Android8.0则让该功能可进一步用于其他Android设备
    2023-01-01
  • asynctask的用法详解

    asynctask的用法详解

    Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行,本文将为您介绍asynctask的用法
    2012-11-11
  • Android自制九宫格解锁控件

    Android自制九宫格解锁控件

    这篇文章主要为大家详细介绍了Android自制九宫格解锁控件的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Android ListView万能适配器实例代码

    Android ListView万能适配器实例代码

    本文主要介绍Android ListView万能适配器,这里整理了详细的资料及实现代码,以及实现效果图,有需要的小伙伴可以参考下
    2016-09-09
  • RxJava之网络请求最常见的三种场景

    RxJava之网络请求最常见的三种场景

    本文想阐述一下当你开发Android应用并采用RxJava作为你的架构,尤其是有关网络请求时最常见的三种场景。这篇文章主要介绍了RxJava之网络请求最常见的三种场景,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Android编程中自定义dialog用法实例

    Android编程中自定义dialog用法实例

    这篇文章主要介绍了Android编程中自定义dialog用法,结合实例形式较为详细的分析了Android自定义dialog的定义.布局及调用等相关技巧,需要的朋友可以参考下
    2015-12-12
  • Android横向进度条演示效果

    Android横向进度条演示效果

    这篇文章主要为大家详细介绍了Android横向进度条的演示效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • Android仿今日头条APP实现下拉导航选择菜单效果

    Android仿今日头条APP实现下拉导航选择菜单效果

    这篇文章主要为大家详细介绍了Android仿今日头条APP实现下拉导航选择菜单效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • WebView 实现全屏播放视频的示例代码

    WebView 实现全屏播放视频的示例代码

    这篇文章主要介绍了WebView 实现全屏播放视频的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • 优化和瘦身Android APK的六个小技巧

    优化和瘦身Android APK的六个小技巧

    Android应用的大小对用户体验和应用性能至关重要,大型APK文件会增加应用的安装时间,启动时间和页面加载时间,降低了用户体验,因此,APK瘦身是Android开发中的重要任务,在本文中,我们将分享6个小技巧,帮助你优化和瘦身Android应用,需要的朋友可以参考下
    2023-11-11

最新评论