2012-04-17 82 views
0

我需要在底部開發一個帶有標籤欄的應用程序,但我有許多Fragments問題,其中Android文檔顯示如何使用TabHost和Fragments。
因爲沒有顯示任何內容,所以關於如何正確使用每個標籤堆棧。爲Android應用程序繪製自定義標籤欄

因爲我可以去tab1它是開放的片段A,那麼我想進入深層和開放的片段B,並有可能切換到tab2,以及切換回看到tab1中相同的片段B.

有一些解決方案爲每個選項卡創建一個FragmentActivity,但是對於該管理您應該使用TabActivity,這已被棄用。

所以,也許我可以繪製我的標籤欄,並把它放在所有的佈局和每次用戶按標籤欄按鈕我剛開始一個活動?

如果這是可能的,也許有些人已經實現了這個,或者可以顯示一些教程如何繪製,或使用它?

謝謝。

回答

4

在XML down_tabs.xml添加此代碼

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/tabhost" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<LinearLayout android:orientation="vertical" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <FrameLayout android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" android:layout_height="0dip" 
     android:layout_weight="1" /> 

    <TabWidget android:id="@android:id/tabs" 
     android:layout_width="fill_parent"  android:layout_height="wrap_content" 
     android:layout_weight="0" /> 
</LinearLayout> 

而且在活動類添加標籤規格,

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.down_tabs); 
    initTabs(); 
} 

private void initTabs() { 
    // Set Calendar Tab 
    // getTabWidget().setDividerDrawable(R.drawable.tab_divider); 
    getTabWidget().setPadding(0, 0, 0, 0); 
    addTab("", R.drawable.home_tab_drawable, CalendarUIActivity.class); 
    addTab("", R.drawable.lucky_dates_drawable, LuckyDatesActivity.class); 
    addTab("", R.drawable.life_stages_drawable, LifeStagesActivity.class); 
    addTab("", R.drawable.find_items_drawable, FindItemsActivity.class); 
    addTab("", R.drawable.more_tab_drawable, MoreActivity.class); 
} 

private void addTab(String labelId, int drawableId, Class<?> targetClass) { 
    TabHost tabHost = getTabHost(); 
    Intent intent = new Intent(this, targetClass); 
    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

    View tabIndicator = LayoutInflater.from(this).inflate(
      R.layout.tab_indicator, getTabWidget(), false); 
    TextView title = (TextView) tabIndicator.findViewById(R.id.title); 
    title.setText(labelId); 
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
    icon.setImageResource(drawableId); 

    tabIndicator.setBackgroundResource(R.drawable.tab_backgroud); 
    // ////////// 
    spec.setIndicator(tabIndicator); 
    spec.setContent(intent); 
    tabHost.addTab(spec); 

} 

我希望它會正常工作。

+0

我曾撰文指出TabActivity是不推薦使用,而應該使用Fragments。我現在採用這種方法,但它不適合我。但是,謝謝。 – Streetboy 2012-04-17 07:53:41

+0

很好的答案謝謝! – 2012-04-23 05:11:07

0

燁良好answer..fragments容易work..u知道..

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@android:id/tabhost" android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <LinearLayout android:orientation="vertical" 
      android:layout_width="fill_parent" android:layout_height="fill_parent"> 

      <FrameLayout android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" android:layout_height="0dip" 
       android:layout_weight="1" /> 

      <TabWidget android:id="@android:id/tabs" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" 
       android:layout_weight="0" /> 
     </LinearLayout> 
    </TabHost> 

完全教程訪問

http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/