Android 使用上下文类膨胀布局
我有两个具有两个相应布局的活动:Android 使用上下文类膨胀布局,android,xml,android-layout,android-activity,layout-inflater,Android,Xml,Android Layout,Android Activity,Layout Inflater,我有两个具有两个相应布局的活动: MainActivity.java->content\u main.xml SecondActivity.java->content\u secondclass.xml My Main Activity会毫无问题地对内容\u secondclass.xml进行膨胀: RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.content_main); LayoutInflater inflat
MainActivity.java
->content\u main.xml
SecondActivity.java
->content\u secondclass.xml
My Main Activity会毫无问题地对内容\u secondclass.xml
进行膨胀:
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.content_main);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.content_secondclass, null);
mainLayout.removeAllViews();
mainLayout.addView(layout);
但是,我如何在SecondActivity.java
的上下文中给出这种膨胀?我不想启动另一个意图,因为它将在新窗口中打开,并且我使用的是导航抽屉式应用程序。我希望在膨胀content\u secondclass.xml时调用SecondActivity,但现在它只是膨胀content\u secondclass,而没有调用SecondActivity.java
My content_secondclass.xml已具有“.SecondActivity”的tools:context
标记
希望我能明白我的意思。非常感谢您在这件事上的时间和帮助。我对您的问题有点了解。我猜您希望在第一个(主)布局中的第二个布局(视图)中进行一些java工作,而无需打开其他活动 最好的方法是使用片段。使用片段,您将拥有灵活的UI,每个片段在一个活动中都有相应的java控制器。如果您是android新手,请阅读片段教程。关于碎片的一些基础知识
致以最诚挚的问候存放导航抽屉的主布局应为框架布局,并创建框架布局的对象和
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.addToBackStack(tag);
transaction.commit();
此处fragment是您创建的片段的对象在google中查找称为“片段”的内容。这不是activty膨胀xml布局的方式,而是在OnCreate上使用setcontentview。感谢你们两位的回答,我们肯定会查看片段。