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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10774656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:54:51  来源:igfitidea点击:

How to display one activity inside another in Android?

androidandroid-activityandroid-linearlayout

提问by AMH

I have one activity and want to display another inside it. Here's my layout:

我有一项活动,想在其中显示另一项活动。这是我的布局:

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

How can I display an Activityin inner LinearLayouton button click?

如何在按钮单击时显示Activity内部LinearLayout

回答by Andrey Ermakov

Use one layout for several activities

为多项活动使用一种布局

Layout instance is inflated for each activity using a setContentViewmethod, usually in onCreate:

布局实例使用一种setContentView方法为每个活动膨胀,通常在onCreate

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

So there's no problem to use the same XMLlayout for different activities.

所以XML对于不同的活动使用相同的布局是没有问题的。

Display one activity inside another

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

You can use a FragmentAPI to complete the task. See full details in developer's guide.

您可以使用FragmentAPI 来完成任务。请参阅开发人员指南中的完整详细信息。

  • 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 MyFragmentclass and load it when appropriate.

  • Create fragments yourself. Do not define the Fragmentin XMLlayout:

    <?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 Activityis created you can add a new Fragmentin this way:

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

    Here MyFragmentis defined like this:

    public class MyFragment extends Fragment {
        ...
    }
    
  • 声明一个这样的布局,Android 会为你创建片段:

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

    然后创建一个MyFragment类并在适当的时候加载它。

  • 自己创建片段。不要定义FragmentinXML布局:

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

    Activity创建父项后,您可以通过Fragment以下方式添加新项:

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

    这里MyFragment是这样定义的:

    public class MyFragment extends Fragment {
        ...
    }
    

If you're targeting below Android 3.0, consider using support packagefor that.

如果您的目标是低于 Android 3.0,请考虑为此使用支持包