Fragment结合FragmentTabHost怎么用

发布时间:2021-10-21 10:15:58 作者:小新
来源:亿速云 阅读:109

这篇文章主要介绍Fragment结合FragmentTabHost怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

FragmentTabHost我也不知道是什么,就是可用用来当新浪微博底部那个状态栏的工具。现在说下怎么使用

首先activity要继承FragmentActivity

例如

public class A extends FragmentActivity{

//定义FragmentTabhost

private FragmentTabhost tab;

private TextView text;

private String str[]={"短信","电话","黑白名单","设置"};

private int icon[]={R.drawable.ic_firewall_sms_desk_setting,R.drawable.firewall_tab_icon_phone,R.drawable.sync_register_icon,R.drawable.img1};

private Class fragments[]={Fragment1.class,Fragment2.class,Fragment3.class,Fragment5.class};

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.harassment_intercepion);//主界面下面放出这个节目的XML文件

iniView();

}

}

=================================================================

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   android:orientation="vertical"

   android:background="#333333"

   >

   <RelativeLayout

       android:layout_width="fill_parent"

   android:layout_height="fill_parent"

   android:id="@+id/up_frame"

       >

   <android.support.v4.app.FragmentTabHost   //这个是tabhost的组件

       android:id="@android:id/tabhost"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

     android:layout_alignParentBottom="true"

       >

       <FrameLayout

           android:id="@android:id/tabcontent"

           android:layout_width="0dp"

           android:layout_height="0dp"

           android:layout_weight="0" />            

   </android.support.v4.app.FragmentTabHost>

   </RelativeLayout>

</RelativeLayout>

==========================================================================

然后实例化组件

public void init()

tab=this.findById(android.R.id.tabhost);//实例化tabhost

//tabhost.setup 第一个参数是context,第二个是拿到父类,第三个是

tabhost.setup(this, getSupportFragmentManager(), R.id.up_frame);外面包围tabhost的Rlayout

//

说说与Fragment结合。

具体使用到下面这个方法

int count=fragments.length;//拿到多少个fragment

for(int i=0;i<count;i++){

TabSpec spec=tab.newTabSpec(str[i]).setIndicator(getTabItemView(i));

tab.addTab(spec,fragments[i],null);//把点击的tab与fragment连接起来

}

}

////////////

private View getTabItemView(int index){

View view =inflater.inflate(R.layout.fragment_down, null);

p_w_picpath=(ImageView) view.findViewById(R.id.fragment_p_w_picpath);

text=(TextView) view.findViewById(R.id.fragment_text);

p_w_picpath.setImageResource(icon[index]);

text.setText(str[index]);

return view;

}

以上是“Fragment结合FragmentTabHost怎么用”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Fragment使用详解
  2. Fragment基本使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

fragmenttabhost

上一篇:android中style怎么用

下一篇:2021年提升编程技能的方式有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》