2014-05-06 19 views
-1

我有3個活動我想使用片段鏈接在一起,因爲TabActivity不再使用。將複雜TabActivity更改爲Fragmet?

我四處張望了一下,瞭解大致的構想,但我不知道如何與片段一起實現整個活動組沒有搞亂原有的外觀和功能...

下面是它如何工作的... 1有2頁,recipe_ingredients.xml和recipe_new.xml。我有一個recipe_tabs.xml鏈接到他們通過RecipeNew.java。 recipe_tabs.xml使用android:tabhost和android:tabcontent來創建制表符併爲製表符提供一個樣式等... 這些製表符並排位於recipe_ingredients.xml和recipe_new.xml之上。突出顯示您的頁面的選項卡,您可以輕鬆地在各頁面之間來回翻轉並突出顯示與您的頁面相對應的選項卡。

這裏是Android清單java的活動: <activity android:name=".RecipeNew"></activity>

要創建標籤和標籤佈局我有一個名爲recipe_tabs

這裏的文件是爲recipe_tabs代碼:(我已將此佈局更改爲FragmentTabHost,只是不知道如何使用片段方法將其全部連接。

<android.support.v4.app.FragmentTabHost 
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:id="@+id/tablinearlayout" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<TabWidget 
android:id="@android:id/tabs" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent" > 
</TabWidget> 
<FrameLayout 
android:id="@android:id/tabcontent" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 
</FrameLayout> 
</LinearLayout> 
</android.support.v4.app.FragmentTabHost> 

這裏是使用tabactivity的java,我想改變爲fragment,但我仍然喜歡它的外觀和功能相同,這可能嗎? 這裏,將不勝感激的RecipeNew.java

public class RecipeNew extends TabActivity implements OnTabChangeListener {    

TabHost _tabHost; 
Resources _res; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.recipe_tabs); 

    tabHost =getTabHost; 
    res = getResources(); 
    tabHost.setOnTabChangedListener(this); 

    TabHost.TabSpec _tabSpec; 

    tabSpec = _tabHost.newTabSpec("recipes").setIndicator("Recipe", res.getDrawable(R.drawable.recipes_tab)).setContent(new Intent(this,RecipeEntry.class)); 
    tabHost.addTab(_tabSpec); 

    tabSpec = _tabHost.newTabSpec("ingredients").setIndicator("Ingredients", res.getDrawable(R.drawable.ingredients_tab)).setContent(new Intent(this.RecipeIngredients.class)); 
    tabHost.addTab(_tabSpec); 

    for(int i=0; i<_tabHost.getTabWidget().getChildCount(); i++) 
    { 
     tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.LTGRAY); 
    } 

    tabHost.getTabWidget(.setCurrentTab(1); 

    tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.DKGRAY); 

} 

public void onTabChanged(String tabI(d) { 
    for(int i=0; i<_tabHost.getTabWidget().getChildCount(); i++) 
    { 
     tabHost.getTabWidget().getChildAt(i).setBackgroundColor (Color.LTGRAY); 
    } 
    tabHost.getTabWidget().getChildAt(_tabHost.getCurrentTab()).setBackgroundColor(Color.GRAY); 
    } 

任何建議或幫助的代碼。最後,佈局應該在頁面頂部呈現2個選項卡,無論您在選項卡中突出顯示哪個頁面,都可以在兩個頁面之間移動,只需點擊選項卡即可。標籤看起來像2個框頁面頂部...外觀它在recipe_tabs中定義

回答

0

看看你的代碼,看起來你的標籤內容是活動(RecipeEntry.class和RecipeIngredients.class)。

只需創建具有相同功能的碎片並按照FragmentTabHost所示的示例代碼進行操作即可。

換句話說,使用FragmentActivity而不是TabActivity,使用Fragments而不是使用Activities作爲標籤內容,然後以相同的方式添加呼叫addTab(...)

+0

謝謝Squonk ..我最終得到的標籤工作在片段! – acro