您好,登錄后才能下訂單哦!
這篇文章主要介紹Fragment結(jié)合FragmentTabHost怎么用,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
FragmentTabHost我也不知道是什么,就是可用用來當(dāng)新浪微博底部那個狀態(tài)欄的工具。現(xiàn)在說下怎么使用
首先activity要繼承FragmentActivity
例如
public class A extends FragmentActivity{
//定義FragmentTabhost
private FragmentTabhost tab;
private TextView text;
private String str[]={"短信","電話","黑白名單","設(shè)置"};
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);//主界面下面放出這個節(jié)目的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>
==========================================================================
然后實(shí)例化組件
public void init()
tab=this.findById(android.R.id.tabhost);//實(shí)例化tabhost
//tabhost.setup 第一個參數(shù)是context,第二個是拿到父類,第三個是
tabhost.setup(this, getSupportFragmentManager(), R.id.up_frame);外面包圍tabhost的Rlayout
//
說說與Fragment結(jié)合。
具體使用到下面這個方法
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);//把點(diǎn)擊的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結(jié)合FragmentTabHost怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。