TopBar のレイアウトを空けて、Topbar を使用してその中に追加しlayout.addView(topbarObject);
ます。ただし、これらの 2 つの 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);