Java 如何在活动中运行片段?
我想在我的地址提醒中运行GoToReminder。你能告诉我怎么做吗?我试着用它,但没用。如果您有任何问题,请随时提问:)Java 如何在活动中运行片段?,java,android,Java,Android,我想在我的地址提醒中运行GoToReminder。你能告诉我怎么做吗?我试着用它,但没用。如果您有任何问题,请随时提问:) 您可以在活动中添加要显示片段的框架布局 在addremement.xml中 <FrameLayout android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="200dp"></FrameLayout> 你必须使用
您可以在活动中添加要显示片段的
框架布局
在addremement.xml中
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="200dp"></FrameLayout>
你必须使用
R.id.container必须在add_rementer2.xml中声明,如下所示:
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
请阅读文档以了解如何使用
另外,你可以通过谷歌阅读一篇文章,你可以添加Android开发者文档ie中提到的片段
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_reminder2);
GoToReminder gotoRemainder = new GoToReminder();
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
这里fragment_container是您要添加片段的布局的id。它可以是框架布局,也可以是您要添加片段的其他布局
这里有更多详细信息:您可以在XML中添加片段并在活动(.java类)中使用它。您能告诉我如何使用吗?好的!我已经贴出了答案。如果对你有帮助的话。你可以接受。@teyon.abdulrehman,请阅读这篇文章是的,但是如果我有滚动视图,我应该在哪里添加框架布局?发布完整的代码或根据你的意愿添加。。要在其中显示片段数据的位置。
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="200dp"></FrameLayout>
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, new GoToReminder()).addToBackStack(null).commit();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.add(R.id.container, GoToReminder.newInstance())
.commit()
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_reminder2);
GoToReminder gotoRemainder = new GoToReminder();
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}