国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android Tabhost使用方法詳解

2019-12-12 06:17:15
字體:
來源:轉載
供稿:網友

Android 實現tab視圖有2種方法,一種是在布局頁面中定義<tabhost>標簽,另一種就是繼承tabactivity.但是我比較喜歡第二種方式,應為如果頁面比較復雜的話你的XML文件會寫得比較龐大,用第二種方式XML頁面相對要簡潔得多。

下面是我的XML源碼:

<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  >  <ListView    android:id="@+id/journals_list_one"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:cacheColorHint="#FFFFFFFF"    android:scrollbars="vertical"    android:paddingTop="5dip"    android:paddingBottom="5dip"    android:paddingRight="5dip"    android:background="#FFFFFFFF"    android:listSelector="@drawable/list_item_selecter"    />  <ListView    android:id="@+id/journals_list_two"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:cacheColorHint="#FFFFFFFF"    android:scrollbars="vertical"    android:paddingTop="5dip"    android:paddingBottom="5dip"    android:paddingRight="5dip"    android:background="#FFFFFFFF"    />  <ListView    android:id="@+id/journals_list_three"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:cacheColorHint="#FFFFFFFF"    android:scrollbars="vertical"    android:paddingTop="5dip"    android:paddingBottom="5dip"    android:paddingRight="5dip"    android:background="#FFFFFFFF"    />  <ListView    android:id="@+id/journals_list_end"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:cacheColorHint="#FFFFFFFF"    android:scrollbars="vertical"    android:paddingTop="5dip"    android:paddingBottom="5dip"    android:paddingRight="5dip"    android:background="#FFFFFFFF"    /> </FrameLayout> 

這是JAVA源碼:

private TabHost tabHost; private ListView listView; private MyListAdapter adapter; private View footerView; private List<Map<String, String>> data = new ArrayList<Map<String, String>>(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  tabHost = this.getTabHost();   LayoutInflater.from(this).inflate(R.layout.main,    tabHost.getTabContentView(), true);   tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("",    getResources().getDrawable(R.drawable.home)).setContent(    R.id.journals_list_one));  tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("",    getResources().getDrawable(R.drawable.activity)).setContent(    R.id.journals_list_two));  tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("",    getResources().getDrawable(R.drawable.community)).setContent(    R.id.journals_list_three));  tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("",    getResources().getDrawable(R.drawable.shop)).setContent(    R.id.journals_list_end));   tabHost.setCurrentTab(0);  setContentView(tabHost);  tabHost.setOnTabChangedListener(tabChangeListener);   showContent();  } 

 讓自己的類繼承TabActivity,然后通過調用getTabHost()方法得到tabhost對象,然后把自己寫好的數據展示的布局文件加載到tabhost中,就可以實現了。最后是通過調用addTab()方法添加標簽的相關屬性(如:標簽名稱,標簽圖片,標簽內容布局)。

而如果通過XML文件配置tabHost則需要注意的是,framelayout,tabwidge標簽的id都必須引用系統的id(@android:id/tabcontent,@android:id/tabs),不然會報異常.在程序用使用findViewById()加載tabhost,然后調用tabhost.setup()方法初始化tabhost,后面的步驟則和上面一種一樣,就不在說明。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陆河县| 永新县| 华池县| 龙川县| 开封县| 井研县| 郎溪县| 牡丹江市| 新密市| 南安市| 台湾省| 沧州市| 岑溪市| 虹口区| 儋州市| 高淳县| 灵山县| 商洛市| 仪征市| 西华县| 绵竹市| 万宁市| 陆丰市| 章丘市| 枝江市| 云阳县| 贺兰县| 板桥市| 时尚| 平定县| 专栏| 隆林| 阿坝县| 平昌县| 固安县| 南开区| 虹口区| 卢龙县| 彝良县| 吉安县| 南和县|