当前位置: 移动技术网 > IT编程>移动开发>Android > Android 使用FragmentTabhost代替Tabhost

Android 使用FragmentTabhost代替Tabhost

2019年07月24日  | 移动技术网IT编程  | 我要评论

女王男奴小说,10244最新地址 2017,xzfz

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 基本上写完了,让我们看看效果!

哈哈,效果还算可以!好了,去吃饭了!

资源下载地址:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网