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。。
供自己参考。
首先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。。
供自己参考。
相关文章推荐
- Tabhost与Fragment的结合使用
- ViewPager跟FragmentTabHost结合起来使用
- Android_横向滑动菜单, HorizontalScrollView,Fragment,Viewpager结合使用
- Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
- 自定义viewpagerindicator和FragmentViewPager结合使用
- 下方显示android.support.v4.app.FragmentTabHost使用总结
- 多层Fragment与ViewPager结合使用
- 使用FragmentTabHost+TabLayout+ViewPager实现双层嵌套Tab
- fragment + tabhost使用
- ActionBar和TabHost结合使用案例
- 使用Fragment实现类似TabHost标签栏的效果
- FragmentTabHost+Fragment的使用
- TabHost使用心得(一)
- Fragment学习之Fragment与menu结合使用
- Android两级导航菜单栏的实现--FragmentTabHost结合ViewPager与Android 开源项目PagerSlidingTabStrip
- 使用FragmentTabhost取代Tabhost
- android FragmentTabHost的使用技巧及注意事项
- 使用FragmentTabHost 完成一个简单的底部导航栏
- Android学习之--底部菜单FragmentTabHost使用
- TabHostFragment使用方法+布局