如何在Android中在一个活动中显示另一个活动?

9

我有一个活动,想在其中显示另一个活动。以下是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

  <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1">
  </LinearLayout>

</LinearLayout>

如何在按钮点击时在内部LinearLayout中显示一个Activity


你可以在另一个LinearLayout中嵌套一个LinearLayout,这没有任何问题。 - Gaurav Agarwal
1个回答

12

使用同一布局来渲染多个活动页面

每个活动页面都可以通过setContentView方法来填充布局实例,通常在onCreate中执行:

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

使用同一份XML布局在不同的活动中没有问题。

在另一个活动中显示一个活动

您可以使用Fragment API来完成此任务。有关完整详情,请参见开发人员指南

  • Declare a layout like that and Android will create fragments for you:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <fragment android:name="com.example.MyFragment"
                android:id="@+id/my_fragment"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" />
    
    </LinearLayout>
    

    Then create a MyFragment class and load it when appropriate.

  • Create fragments yourself. Do not define the Fragment in XML layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/my_parent_layout">
    
    </LinearLayout>
    

    After your parent Activity is created you can add a new Fragment in this way:

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

    Here MyFragment is defined like this:

    public class MyFragment extends Fragment {
        ...
    }
    
如果您的目标是 Android 3.0 以下版本,请考虑使用 支持包

你能给我一个动态添加的例子吗? - AMH
<fragment android:name="com.example.news.ArticleReaderFragment" android:id="@+id/viewer" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" />我还有另一个名为(test)的活动,这段代码正确吗? FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); test fragment = new test(); fragmentTransaction.add(R.layout.viewer, test); fragmentTransaction.commit(); 这是正确的吗? - AMH
@AMH 你应该扩展 Fragment 而不是 Activity,即 class test extends Fragment。然后在 XML 中给你的父级 LinearLayout 分配一个ID,如下所示调用它。我在答案中添加了一个示例。 - Andrey Ermakov
14
这里描述了如何将一个Fragment添加到一个Activity,而不是将一个Activity添加到另一个Activity中。 - user153275
1
这里描述了如何将一个片段添加到一个活动中,而不是将一个活动添加到另一个活动中。 - IntoTheDeep
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接