Android 以编程方式包括布局
我的android应用程序中有3个窗口。因此,我认为将有3项活动。还有一个导航抽屉。每个活动都必须提供导航抽屉。我在Android Studio中使用了模式导航抽屉活动。有3种xml布局: 1) 。activity_main.xml–具有导航视图,包括app_bar_main.xml 2) 。app_bar_main.xml–包括content_main.xml 3) 。content_main.xml–下面是一些内容(文本、图片等) 当然,每项活动都有不同的内容。因此,每个活动都有自己的内容_main.xml。我需要以编程方式更改app_bar_main.xml(而不是activity_main.xml)中包含的布局。我试过这个:Android 以编程方式包括布局,android,android-layout,include,android-xml,Android,Android Layout,Include,Android Xml,我的android应用程序中有3个窗口。因此,我认为将有3项活动。还有一个导航抽屉。每个活动都必须提供导航抽屉。我在Android Studio中使用了模式导航抽屉活动。有3种xml布局: 1) 。activity_main.xml–具有导航视图,包括app_bar_main.xml 2) 。app_bar_main.xml–包括content_main.xml 3) 。content_main.xml–下面是一些内容(文本、图片等) 当然,每项活动都有不同的内容。因此,每个活动都有自己的内容_
CoordinatorLayout main布局=(CoordinatorLayout)findViewById(R.id.app\u bar\u布局);
LayoutInflater LayoutInflater=(LayoutInflater)this.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
mainLayout.addView(1,LayoutFlater.充气(R.layout.content_main,this,false));
但最后一行加了下划线,表示“无法解析方法inflate()”
我也试过:
CoordinatorLayout main布局=(CoordinatorLayout)findViewById(R.id.app\u bar\u布局);
View child=GetLayoutFlater().充气(R.layout.content\u main,null);
mainLayout.addView(子级);
但这会在工具栏标题上显示content_main.xml的文本。因此,一个文本覆盖另一个文本
我做错了什么?尝试设置子视图布局参数,如:
CoordinatorLayout mainLayout = (CoordinatorLayout) findViewById(R.id.app_bar_layout);
View child = getLayoutInflater().inflate(R.layout.content_main, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(WEIGHT, HEIGHT);
params.setMargins(MARGIN, MARGIN, MARGIN, MARGIN);
child.setLayoutParams(params);
mainLayout.addView(child);
你可以看看。它将帮助您在同一活动中选择各种布局,无论何时调用它们。此外,您还可以重复使用导航抽屉。谢谢。这是可行的,但似乎风险太大,因为屏幕大小不一。也许使用片段比@NothingWhather写的“include”更好?