日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

FragmentTabHost + Fragment 使用小记

發(fā)布時間:2025/5/22 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 FragmentTabHost + Fragment 使用小记 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

  由于業(yè)務需要,需要在使用Activity的頂部使用一個導航欄,點擊導航欄的tab,下面顯示內(nèi)容。決定采用項目中已經(jīng)使用過的FragmentTabHost + Fragment的方式實現(xiàn)。不同的是之前的FragmentTabHost位于底部(下面統(tǒng)稱:Bottom),現(xiàn)在需要放置在頂部(下面統(tǒng)稱: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>

結果為:

下面的內(nèi)容,無論tab切換到哪一個,都是空白。針對這種情況,進行斷點分析,發(fā)現(xiàn)Fragment中的View控件都已經(jīng)被加載,只是被遮擋住了。

因此,問題應該出在布局上。

  偶然的將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可以,最終的原因,待研究清楚再總結。

轉(zhuǎn)載于:https://www.cnblogs.com/ithaibo-sit/p/6023316.html

總結

以上是生活随笔為你收集整理的FragmentTabHost + Fragment 使用小记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。