Android Tabhost使用方法詳解
Android 實現(xiàn)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,然后通過調(diào)用getTabHost()方法得到tabhost對象,然后把自己寫好的數(shù)據(jù)展示的布局文件加載到tabhost中,就可以實現(xiàn)了。最后是通過調(diào)用addTab()方法添加標簽的相關(guān)屬性(如:標簽名稱,標簽圖片,標簽內(nèi)容布局)。
而如果通過XML文件配置tabHost則需要注意的是,framelayout,tabwidge標簽的id都必須引用系統(tǒng)的id(@android:id/tabcontent,@android:id/tabs),不然會報異常.在程序用使用findViewById()加載tabhost,然后調(diào)用tabhost.setup()方法初始化tabhost,后面的步驟則和上面一種一樣,就不在說明。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android組件TabHost實現(xiàn)頁面中多個選項卡切換效果
- android TabHost(選項卡)的使用方法
- android 選項卡(TabHost)如何放置在屏幕的底部
- Android組件必學之TabHost使用方法詳解
- Android控件之TabHost用法實例分析
- Android 使用FragmentTabhost代替Tabhost
- 詳解Android應用中使用TabHost組件進行布局的基本方法
- Android編程實現(xiàn)設(shè)置TabHost當中字體的方法
- 詳解Android TabHost的多種實現(xiàn)方法 附源碼下載
- Android TabHost組件使用方法詳解
- Android TabHost選項卡標簽圖標始終不出現(xiàn)的解決方法
相關(guān)文章
Android 日志系統(tǒng)Logger源代碼詳細介紹
本文主要介紹Android 日志系統(tǒng)Logger,這里整理了關(guān)于Android源碼的日志系統(tǒng)資料,有研究Android源碼的朋友可以參考下2016-08-08Android?ViewPager2?+?Fragment?聯(lián)動效果的實現(xiàn)思路
這篇文章主要介紹了Android?ViewPager2?+?Fragment?聯(lián)動,本篇主要介紹一下 ViewPager2 + Fragment聯(lián)動效果的實現(xiàn)思路,需要的朋友可以參考下2022-12-12Android自定義View實現(xiàn)loading動畫加載效果
項目開發(fā)中對Loading的處理是比較常見的,安卓系統(tǒng)提供的不太美觀,引入第三發(fā)又太麻煩,這時候自己定義View來實現(xiàn)這個效果。這篇文章主要介紹了Android自定義View實現(xiàn)loading動畫加載效果,需要的朋友可以參考下2017-03-03Android ViewPager實現(xiàn)智能無限循環(huán)滾動回繞效果
這篇文章主要為大家詳細介紹了Android ViewPager實現(xiàn)智能無限循環(huán)滾動回繞效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07在RecyclerView中實現(xiàn)button的跳轉(zhuǎn)功能
本次實驗就是在RecyclerView中添加一個button控件并實現(xiàn)監(jiān)聽,使鼠標點擊時可以跳轉(zhuǎn)到另外一個設(shè)計好的界面,對RecyclerView實現(xiàn)button跳轉(zhuǎn)功能感興趣的朋友一起看看吧2021-10-10