如何在 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
How to display one activity inside another in Android?
提问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 Activity
in inner LinearLayout
on button click?
如何在按钮单击时显示Activity
内部LinearLayout
?
回答by Andrey Ermakov
Use one layout for several activities
为多项活动使用一种布局
Layout instance is inflated for each activity using a setContentView
method, 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 XML
layout for different activities.
所以XML
对于不同的活动使用相同的布局是没有问题的。
Display one activity inside another
在另一个活动中显示一个活动
You can use a Fragment
API to complete the task. See full details in developer's guide.
您可以使用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
inXML
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 newFragment
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 会为你创建片段:
<?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
类并在适当的时候加载它。自己创建片段。不要定义
Fragment
inXML
布局:<?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,请考虑为此使用支持包。