如何在 FrameLayout 中加入新的意图



我在安卓中有一个标签菜单:

    <View
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:alpha="0.5"
    />
    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="10dp" >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="0dp"
                android:layout_marginTop="0dp"
                android:drawablePadding="0dip"
                android:paddingLeft="0dip"
                android:paddingRight="0dip"
                android:paddingStart="0dp" />
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </LinearLayout>
    </TabHost>

我想开始一个新的意图并将其放在框架布局中(当单击相应的选项卡时):

我试过这样:

Intent i = new Intent(this, tab1.class);
startActivity(i);

但结果是一个隐藏选项卡菜单的新活动。

你应该为此使用片段:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.tabcontent, fragment);
fragmentTransaction.commit();

c.f. Android 开发者 API 片段指南

相关内容

最新更新