2015-04-08 45 views
3

在文檔中說TabActivity已棄用:enter link description here。我遵循他們的例子使用FragmentTabHost,但它也崩潰了。所以我嘗試創建一個簡單的activity並添加一個intent作爲TabSpec的內容,但該應用崩潰了!那麼如何創建一個包含ListViewTabHost如何創建一個包含ListView的tabHost?

這裏是我做過什麼:

@Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.onglet_parcelle_batiment); 

     mTabHost = (TabHost)findViewById(R.id.onglets); 

     mTabHost.setup(); 

     Bundle dataSent = getIntent().getExtras(); 

     bien_code = dataSent.getString("bien_code"); 

     afficherOngletParcelle(); 

     afficherOngletBatiments(); 

     mTabHost.setCurrentTab(0); 

    } 

    private void afficherOngletParcelle() { 

     TabHost.TabSpec parcelle = mTabHost.newTabSpec("parcelle"); 
     View onglet = getLayoutInflater().inflate(R.layout.template_tab_onglets, null); 
     TextView label = (TextView) onglet.findViewById(R.id.tabLabel); 
     label.setText(getResources().getString(R.string.parcelle)); 
     parcelle.setIndicator(onglet); 
     Intent i = new Intent(OngletParcelleListeBatiments.this, ParcelleActivity.class); 
     i = i.putExtra("bien_code", bien_code); 
     parcelle.setContent(i); 
     mTabHost.addTab(parcelle); 

    } 

    private void afficherOngletBatiments() { 

     TabHost.TabSpec batiment = mTabHost.newTabSpec("batiments"); 
     View onglet = getLayoutInflater().inflate(R.layout.template_tab_onglets, null); 
     TextView label = (TextView) onglet.findViewById(R.id.tabLabel); 
     label.setText(getResources().getString(R.string.titre_liste_batiments)); 
     batiment.setIndicator(onglet); 
     Intent i = new Intent(OngletParcelleListeBatiments.this, ListeBatimentsActivity.class); 
     i = i.putExtra("bien_code", bien_code); 
     batiment.setContent(i); 
     mTabHost.addTab(batiment); 

    } 
+0

你可以發佈你迄今爲止所做的? –

回答

5

我很高興你問這個問題,因爲我一直在努力超過1天試圖達到你想達到不使用TabActivity什麼。但我終於做到了。我爲自己製作了一個教程,我將來會在我的博客中發佈它。

下面是它的詳細信息: -

1)在你的XML文件,添加了3個標籤下面。

<TabHost 
     android:id="@+id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="10dp" 
     android:background="@android:color/white"> 

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

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
       <!-- tab N°1 --> <!-- Inclusion of the interface --> 
       <include 
        android:id="@+id/i_layout_1" 
        layout="@layout/layout_1" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" /> 
       <!-- tab N°2 --> <!-- Inclusion of the interface --> 
       <include 
        android:id="@+id/i_layout_2" 
        layout="@layout/layout_2" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/> 
       <!-- tab N°3 --> <!-- Inclusion of the interface --> 
       <include 
        android:id="@+id/i_layout_3" 
        layout="@layout/layout_3" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 
      </FrameLayout> 

     </LinearLayout> 
    </TabHost> 

2)創建爲每個標籤的佈局你在包括部件的參考。例如「layout_1.xml」。

你可以人在這裏在此佈局

<?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" 
android:background="@android:color/white" 
android:padding="10dp"> 

<TextView 
    android:id="@+id/my_textview" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Example" 
    android:textColor="@android:color/black" /> 

</RelativeLayout> 

3)添加ListView或任何使用必需的規範,例如自定義指標:

<?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" 
android:background="@drawable/business_tabs_indicator_background" 
android:padding="10dp"> 

<TextView 
    android:id="@+id/tv_indicator_label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:gravity="center" 
    android:textColor="@android:color/black" 
    android:textStyle="bold" /> 

</RelativeLayout> 

4)創建自定義選項卡,用於選項卡指示符佈局以處理不同的選擇狀態,重點,..等。例如

business_tabs_indicator_background.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- Active tab --> 
<item android:drawable="@android:color/darker_gray" android:state_focused="false" android:state_pressed="false" android:state_selected="true" /> 
<!-- Inactive tab --> 
<item android:drawable="@android:color/white" android:state_focused="false" android:state_pressed="false" android:state_selected="false" /> 
<!-- Pressed tab --> 
<item android:drawable="@android:color/darker_gray" android:state_pressed="true" /> 
<!-- Selected tab (using d-pad) --> 
<item android:drawable="@android:color/darker_gray" android:state_focused="true" android:state_pressed="false" android:state_selected="true" /> 

</selector> 

5)在您的活動或片段,初始化TabHost並添加相應的標籤,如下:

private void initializeTabHoster(View rootview) { 
    tabHost = (TabHost) rootview.findViewById(R.id.tabhost); 
    //Important 
    tabHost.setup(); 

    TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab Tag"); 
    TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab Tag"); 
    TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab Tag"); 

    // Set the Tab name and Activity 
    // that will be opened when particular Tab will be selected 

    tab1.setIndicator(createTabIndicator("Tab1")); 
    tab1.setContent(R.id.i_layout_1); 

    tab2.setIndicator(createTabIndicator("Tab2")); 
    tab2.setContent(R.id.i_layout_2); 

    tab3.setIndicator(createTabIndicator("Tab3")); 
    tab3.setContent(R.id.i_layout_3); 

    /** Add the tabs to the TabHost to display. */ 
    tabHost.addTab(tab1); 
    tabHost.addTab(tab2); 
    tabHost.addTab(tab3); 
} 

private View createTabIndicator(String text) { 
    View view = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, null); 
    TextView textView = (TextView) view.findViewById(R.id.tv_indicator_label); 
    textView.setText(text); 
    return view; 
} 

我希望這將幫你。隨意問任何問題:)

+0

在第3步中,我是否必須創建「自定義指標」?它是強制性的嗎? – pheromix

+0

@pheromix不,它不是強制性的。但是,這將允許您創建一個自定義選項卡包含帶有文本,圖標或其他任何東西的圖標:)。但我不是強制性的** –

+0

好吧,它是用於'TabWidget'上的選項卡? – pheromix