您的位置:首页 > 其它

Fragment 结合FragmentTabHost使用心得

2014-05-06 00:24 351 查看
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);
image=(ImageView) view.findViewById(R.id.fragment_image);
text=(TextView) view.findViewById(R.id.fragment_text);
image.setImageResource(icon[index]);
text.setText(str[index]);
return view;
}
==========================================
总的来说就是
把fragment添加到fragmenttabhot。。
供自己参考。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: