有一个 TopBar 的 Layout 空置,并通过使用添加 Your Topbarlayout.addView(topbarObject);
关于您的第二个问题,据我所知,setContentView 只能调用一次。但是,您可以在需要时将这两个 xml 文件使用View.inflate(other_content.xml)
并添加到父 xml 布局中。您可以removeView()
在父布局上并addView()
使用新的布局文件。
编辑:对于这两个问题的解决方案,您可以有一个父布局,例如。如下所示:
//Omitting the obvious tags
//parent.xml
<RelativeLayout
android:id="@+id/parentLayout">
<RelativeLayout
android:id="@+id/topLayout">
</RelativeLayout>
<RelativeLayout
android:id="@+id/contentLayout">
</RelativeLayout>
</RelativeLayout>
现在在您的代码中将父布局设置为内容视图,创建一个 TopBar 布局对象并将其添加到 topLayout。
setContentView(R.layout.parent);
MyTopBar topBar=new MyTopBar(this);
RelativeLayout toplayout=(RelativeLayout)findViewByid(R.id.topLayout);
topLayout.addView(topBar); //or you can directly add it to the parentLayout, but it won't work for the first question. So better stick to it.
现在膨胀所需的 xml 布局。并将其添加到 contentLayout。
RelativeLayout layout=(RelativeLayout)View.inflate(R.layout.content,null);
contentLayout.addView(layout);//Assuming you've done the findViewById on this.
而当您需要显示其他内容xml时,只需调用以下代码即可。
contentLayout.removeAllView();
RelativeLayout layout2=(RelativeLayout)View.inflate(R.layout.other_content,null);
contentLayout.addView(layout2);