2015-02-07 52 views
0

我試圖實現與Instagram應用類似外觀和感覺的五個選項卡。現在對TabHost和TabActivity的棄用最佳做法是什麼?如何在Android中實現製表符現在TabHosts已棄用?

我想在屏幕頂部有五個選項卡(在操作欄下方),每個選項卡都有一個可繪製的代表該選項卡的內容。

由於標籤將始終顯示,我希望他們在一個活動。然而每個標籤的內容將取決於哪個標籤被按下。因此,我希望在放入片段的選項卡下的同一Activity中顯示一個視圖,一次一個,具體取決於按哪個片段。

所以總結: 我想有一個活性在頂部的標籤和它們下方的視圖,其中我加載的五個不同片段一次一個。

請幫我做這一切成爲可能,而無需使用過時的類:)

+0

檢查這個答案也許? http://stackoverflow.com/questions/10297293/android-how-can-i-replace-the-deprecated-tabhost – miselking 2015-02-07 15:21:52

回答

0

我想實現五個選項卡中相似的外觀和感覺作爲Instagram的應用程序

因爲不是每個人都使用Instagram的,在將來,請張貼你想要的東西的截圖,並從你的問題鏈接到它。

我想有一個活動與頂部的標籤和下方的視圖,其中我一次加載五個不同的片段之一。

Use FragmentTabHost。或者,使用ViewPager和選項卡式指示符(例如,TabPageIndicatorthe ViewPagerIndicator library中的IconPagerAdapter)。

+0

在FragmentTabHost-API,它說: 「你將無法正常使用,而不是使用操作欄選項卡「。 這是不好的做法嗎?你有沒有成功實施FragmentTabHost的例子?:) – epoxy 2015-02-10 09:43:52

+0

@epoxy:「這是不好的做法嗎?」 - 文檔中的評論已過時。大約9個月前,我提交了一個關於該問題的文章(https://code.google.com/p/android/issues/detail?id=73538)。 「你有沒有成功實現FragmentTabHost的例子?:)」 - 不,因爲大多數人都在使用'ViewPager'和一個選項卡式指示器。 – CommonsWare 2015-02-10 11:46:21

1

您可以使用此兩個新類來創建標籤SlidingTabLayoutSlidingTabStrip,你必須用一些片斷傳遞ViewPager這個類,對每個選項卡使用setCustomTabView自定義視圖。你可以找到谷歌的例子。

編輯:

自定義佈局(我同時包含圖片和文字)設置你的標籤是這樣的:

slideTab = (SlidingTabLayout) findViewById(R.id.getbook_sliding_tabs); 
slideTab.setDistributeEvenly(true); 
slideTab.setCustomTabView(R.layout.cat_tab_layout, R.id.main_tab_title); 

cat_tab_layout

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="6dip" 
    android:background="@drawable/cat_tab_background"> 

    <ImageView 
     android:id="@+id/main_tab_image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/cat_all_book_icon" 
     android:layout_centerHorizontal="true"/> 

    <TextView 
     android:id="@+id/main_tab_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="tab1" 
     android:textColor="@color/medium_gray" 
     android:layout_below="@id/main_tab_image" 
     android:layout_centerHorizontal="true"/> 

</RelativeLayout> 

做一些變化populateTabStrip()形式SlidingTabLayout等級:

adapter = (GetBookFragmentPagesAdapter)mViewPager.getAdapter();// get your view pager adapter 
for (int i = 0; i < adapter.getCount(); i++) { 
.... 
if (mTabViewLayoutId != 0) { 
    // If there is a custom tab view layout id set, try and inflate it 
    tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false); 
    tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId); 

    ImageView tabImage = (ImageView)tabView.findViewById(R.id.main_tab_image); 
    tabImage.setImageResource(adapter.getImages(i)); // getImage() return image id for each tab 

} 

....

,那麼你將最終的東西是這樣的:

enter image description here

+0

SlidingTabLayout是否允許我將標籤從文本更改爲圖標?我找不到這樣做的例子。如果這是可能的,這個解決方案是間歇性的。 – epoxy 2015-02-10 09:48:58

+0

@epoxy:當然你可以把任何自定義佈局,我已經在我的應用程序:),請參閱我編輯的答案。 – mehdok 2015-02-10 10:14:29