2013-08-30 48 views
6

當前我正在嘗試爲我的項目實現FragmentTabHost。我在這個片段上仍然是新的,但是我發現它在重用佈局等方面非常出色,這就是爲什麼我想要進一步推進自己。現在,我讀了如何創建片段選項卡教程,我來到這個教程:自定義FragmentTabHost在底部設置TabWidget

http://maxalley.wordpress.com/2013/05/18/android-creating-a-tab-layout-with-fragmenttabhost-and-fragments/

現在,這工作得很好,除了tabWidget是我的佈局,我想它的頂部在底部。我發現我需要設置tabWidget所有選項卡已初始化後,所以我嘗試添加這些代碼:

mTabWidget = (TabWidget) findViewById(android.R.id.tabs); 

    mTabWidget.setBackgroundColor(Color.WHITE); 
    mTabWidget.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE); 
    mTabWidget.setGravity(Gravity.BOTTOM); 

現在這一個已經消除了分頻器和改變顏色,但顯然不會把我的Widget在我的佈局的底部。現在我該怎麼做?

我也嘗試編輯Tabhost xml,並將TabWidget放在FrameLayout之後,但沒有任何反應。這裏的XML:

<android.support.v4.app.FragmentTabHost 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@android:id/tabhost" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

      <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical" > 

       <FrameLayout 
         android:id="@+id/tabFrameLayout" 
         android:layout_width="match_parent" 
         android:layout_height="0dp" 
         android:layout_weight="1" /> 

       <TabWidget 
         android:id="@android:id/tabs" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="0" 
         android:orientation="horizontal" 
         /> 

      </LinearLayout> 

     </android.support.v4.app.FragmentTabHost> 

回答

13

我有指此鏈接github example

這將是您的佈局:

<?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" > 

    <FrameLayout 
     android:id="@+id/realtabcontent" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" /> 

    <android.support.v4.app.FragmentTabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="0" /> 
    </android.support.v4.app.FragmentTabHost> 

</LinearLayout> 

爲您的自定義標籤:

mTabHost.addTab(setIndicator(mTabHost.newTabSpec("Tab1"), 
         R.drawable.image1), 

    public TabSpec setIndicator(Context ctx,TabSpec spec, int resid) { 
     // TODO Auto-generated method stub 
     View v = LayoutInflater.from(ctx).inflate(R.layout.tabs_text, null); 
     v.setBackgroundResource(resid); 
     TextView text = (TextView) v.findViewById(R.id.tab_title); 
     text.setText(spec.getTag()); 
     return spec.setIndicator(v); 
    } 

編輯

//To set drawable to your perticular TAB 
mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_login); 

末編輯

如果你想添加繪製(選擇):

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/tab_compose_h" android:state_selected="true"/> 
    <item android:drawable="@drawable/tab_compose_h" android:state_pressed="true"/> 
    <item android:drawable="@drawable/tab_compose"/> 

</selector> 
+0

目前檢查出來。 :) – KaHeL

+0

嗨Gru,測試它,是的它的工作!但我有一個問題。 tabDividers顯示,我不知道將它關閉。以及我不能改變標籤的顏色。我應該怎麼做? – KaHeL

+0

我已經得到它! :)) 非常感謝! – KaHeL