Java 将复杂活动更改为片段?

Java 将复杂活动更改为片段?,java,android,xml,android-fragments,tabs,Java,Android,Xml,Android Fragments,Tabs,我有3个活动想使用片段链接在一起,因为不再使用TabActivity 我环顾了一下四周,了解了总体思路,但不确定如何在不破坏原始外观和功能的情况下,将整个活动组与片段一起实施 下面是它的工作原理1有两个页面,分别是recipe\u components.xml和recipe\u new.xml。我有一个配方\u tabs.xml通过RecipeNew.java链接到它们。recipe_tabs.xml使用android:tabhost和android:tabcontent创建选项卡并为选项卡提供

我有3个活动想使用片段链接在一起,因为不再使用TabActivity

我环顾了一下四周,了解了总体思路,但不确定如何在不破坏原始外观和功能的情况下,将整个活动组与片段一起实施

下面是它的工作原理1有两个页面,分别是recipe\u components.xml和recipe\u new.xml。我有一个配方\u tabs.xml通过RecipeNew.java链接到它们。recipe_tabs.xml使用android:tabhost和android:tabcontent创建选项卡并为选项卡提供样式等选项卡并排放置在recipe_components.xml和recipe_new.xml的顶部。您所在页面的选项卡高亮显示,您可以轻松地在页面之间来回翻转,并且选项卡高亮显示与您所在页面对应的内容

以下是android清单中的java活动:

要创建选项卡和选项卡布局,我有一个名为recipe_Tabs的文件

以下是配方选项卡的代码:(我已经将其中的布局更改为FragmentTabHost,只是不确定如何使用fragment方法连接所有选项卡。

<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,我想将其更改为片段,但我仍然希望它的外观和功能保持不变,这可能吗? 下面是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 );
    }
公共类RecipeNew扩展TabActivity实现OnTabChangeListener{
TabHost(TabHost);;
资源;;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe_选项卡);
tabHost=getTabHost;
res=getResources();
tabHost.setOnTabChangedListener(此);
TabHost.TabSpec\u TabSpec;
tabSpec=_tabHost.newTabSpec(“recipes”).setIndicator(“Recipe”,res.getDrawable(R.drawable.recipes_tab)).setContent(newintent(this,RecipeEntry.class));
tabHost.addTab(\u tabSpec);
tabSpec=_tabHost.newTabSpec(“配料”).setIndicator(“配料”,res.getDrawable(R.drawable.components_tab)).setContent(newintent(this.recipeingedints.class));
tabHost.addTab(\u tabSpec);

对于(int i=0;i查看您的代码,您的选项卡内容似乎是活动(RecipeEntry.class和RecipeIngElements.class)

只需创建具有相同功能的片段,并遵循所示的示例代码


换句话说,使用FragmentActivity而不是TabActivity,使用Fragments而不是Activities作为选项卡内容,然后以相同的方式添加call
addTab(…)

谢谢Squonk..我最终让选项卡在Fragments中工作!