【问题标题】:Changing Complex TabActivity into Fragment?将复杂的 TabActivity 更改为片段?
【发布时间】:2014-05-06 00:57:13
【问题描述】:

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

我环顾了一下,了解了大致的想法,但不确定如何在不破坏原始外观和功能的情况下将整个活动组与片段一起实现......

这是它的工作原理... 1 有 2 个页面,recipe_ingredients.xml 和 recipe_new.xml。我有一个通过 RecipeNew.java 链接到它们的 recipe_tabs.xml。 recipe_tabs.xml 使用 android:tabhost 和 android:tabcontent 创建选项卡并为选项卡提供样式等... 选项卡并排放置在 recipe_ingredients.xml 和 recipe_new.xml 之上 .您所在页面的标签会突出显示,您可以轻松地在页面之间来回切换,标签突出显示与您所在的页面相对应。

这是 android manifest 中的 java 活动: <activity android:name=".RecipeNew"></activity>

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

这里是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 );
    }

任何建议或帮助将不胜感激。最后,布局应该在页面顶部呈现 2 个选项卡,无论您在选项卡中突出显示哪个页面,您都可以在两个页面之间移动,您只需点击选项卡即可。选项卡看起来像 2 个框页面顶部...外观它在recipe_tabs中定义

【问题讨论】:

    标签: java android xml android-fragments tabs


    【解决方案1】:

    查看您的代码,您的选项卡内容似乎是活动(RecipeEntry.class 和 RecipeIngredients.class)。

    只需创建具有相同功能的片段并按照FragmentTabHost 显示的示例代码进行操作即可。

    也就是说,使用 FragmentActivity 代替 TabActivity,使用 Fragments 代替Activity 作为选项卡内容,然后以相同的方式添加调用addTab(...)

    【讨论】:

    • 感谢 Squonk.. 我最终让标签以碎片形式工作!
    猜你喜欢
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    相关资源
    最近更新 更多