Android 如何像setContentView一样设置FrameLayout的内容
我想要像setContentView这样的东西,但是对于我当前布局中的FrameLayout,这意味着我在我的主活动布局中有一些控件,其中一个是pageHolder,它是一个FrameLayout。我在res/layout中添加了一个android布局,它也是名为page1的FrameLayout和另一个名为page2的FrameLayout。我想在应用程序运行时将pageHolder的内容更改为page1或page2的内容 我执行以下代码,但它给了我错误:Android 如何像setContentView一样设置FrameLayout的内容,android,layout,Android,Layout,我想要像setContentView这样的东西,但是对于我当前布局中的FrameLayout,这意味着我在我的主活动布局中有一些控件,其中一个是pageHolder,它是一个FrameLayout。我在res/layout中添加了一个android布局,它也是名为page1的FrameLayout和另一个名为page2的FrameLayout。我想在应用程序运行时将pageHolder的内容更改为page1或page2的内容 我执行以下代码,但它给了我错误: FrameLayout pa
FrameLayout pageHolder = (FrameLayout)findViewById(R.id.pageHolder);
FrameLayout page1 = (FrameLayout)findViewById(R.layout.page1);
pageHolder.removeAllViews();
pageHolder.addView(page1);
它说page1是空的,我自己检查过,它是空的。你能帮帮我吗?我觉得你应该用LayoutInflater给page1的布局充气。它应该是这样的:
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.page1, null);
在我看来,您应该使用LayoutInflater来为page1布局充气。它应该是这样的:
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.page1, null);
你想在另一个FrameLayout中放置一个FrameLayout?没错,我可能尝试过,但这里的问题是page1为null意味着它不是使用res/layout中的xml文件初始化的。你的布局xml文件中有一个android:id=@+id/page1的视图吗?所以你想在另一个FrameLayout中放置一个FrameLayout?没错,可能我试过了,但这里的问题是page1为null意味着它不是使用res/layoutIs中的xml文件初始化的。您的布局xml文件中是否有一个带有android:id=@+id/page1的视图?而不是context.getSystemServiceContext.layou\u INFLATER\u服务,您可以只编写getlayoutinflaterin而不是context.getSystemServiceContext.LAYOUT\u充气器\u服务您可以只编写getLayoutInflater