【问题标题】:how to put a new intent in FrameLayout如何在 FrameLayout 中添加新意图
【发布时间】:2016-03-01 12:21:47
【问题描述】:

我在 android 中有一个选项卡菜单:

    <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>

我想开始一个新的意图并将其放在 FrameLayout 中(当点击相应的选项卡时):

我试过这样:

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

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

【问题讨论】:

    标签: android android-layout android-intent


    【解决方案1】:

    您应该为此使用片段:

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

    参考Android Developers API Guides on Fragments

    【讨论】:

    • 因此,标签 1、2、... 会有不同的片段,只需使用 fragmentTransaction.add(R.id.tabcontent, fragment) 将它们添加到布局中或使用 fragmentTransaction.replace(R.id.tabcontent, fragment) 替换现有内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多