FragmentTabHost + Fragment 使用小记
生活随笔
收集整理的這篇文章主要介紹了
FragmentTabHost + Fragment 使用小记
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
由于業務需要,需要在使用Activity的頂部使用一個導航欄,點擊導航欄的tab,下面顯示內容。決定采用項目中已經使用過的FragmentTabHost + Fragment的方式實現。不同的是之前的FragmentTabHost位于底部(下面統稱:Bottom),現在需要放置在頂部(下面統稱:Top)。下面將對這兩種方式進行比較:
Bottom:
Activity布局文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/activity_main" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" > 7 8 <FrameLayout 9 android:id="@+id/content_main" 10 android:layout_width="match_parent" 11 android:layout_height="0dp" 12 android:layout_weight="1" 13 android:background="@color/white"/> 14 15 <android.support.v4.app.FragmentTabHost 16 android:id="@+id/tabHost_main" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:layout_marginTop="5dp"> 20 <FrameLayout 21 android:id="@+id/tabContent" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content"/> 24 </android.support.v4.app.FragmentTabHost> 25 26 </LinearLayout>
Activity中設置FragmentTabHost:
protected void initTabs() {fm = getSupportFragmentManager();layoutInflater = LayoutInflater.from(this);tabLabels = new String[]{CommonUtil.getStringFromRes(this, R.string.text_tab_promotion),CommonUtil.getStringFromRes(this, R.string.text_tab_order),CommonUtil.getStringFromRes(this, R.string.text_tab_personal)};tabHost.setup(this, fm, R.id.content_main);for (int i=0; i<fragments.length; i++) {TabHost.TabSpec tabSpec = tabHost.newTabSpec(tabLabels[i]).setIndicator(getTabItemView(i));tabHost.addTab(tabSpec, fragments[i], null);tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.white);}tabHost.getTabWidget().setDividerDrawable(null);}顯示正常
?
Top:
top方式Activity中對FragmentTabHost的設置跟上面一樣,因此只對Layout做對比。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><android.support.v4.app.FragmentTabHostandroid:id="@+id/tab_host"android:layout_width="match_parent"android:layout_height="40dp"/><FrameLayoutandroid:id="@+id/fragment_content"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>結果為:
下面的內容,無論tab切換到哪一個,都是空白。針對這種情況,進行斷點分析,發現Fragment中的View控件都已經被加載,只是被遮擋住了。
因此,問題應該出在布局上。
偶然的將LinearLayout換成RelativeLayout
<?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"><FrameLayoutandroid:layout_marginTop="42dp"android:id="@+id/fragment_content"android:layout_width="match_parent"android:layout_height="match_parent"/><android.support.v4.app.FragmentTabHostandroid:id="@+id/tab_host"android:layout_width="match_parent"android:layout_height="40dp"/></RelativeLayout>成功:
?
這里為什么使用LinlearLayout不成功,換成RelativeLayout可以,最終的原因,待研究清楚再總結。
轉載于:https://www.cnblogs.com/ithaibo-sit/p/6023316.html
總結
以上是生活随笔為你收集整理的FragmentTabHost + Fragment 使用小记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【JZOJ4835】【GDOI2017模
- 下一篇: 鼎烹世家和太太乐鸡精哪个好?