拡張は正しい方法です。setContentViewを正しい方法でオーバーライドするだけです。これが実際の例ですが、ドロワーの代わりに、作成したカスタムタブバーを使用します。
次のように、引き出しを使用してレイアウトを定義します。
これはact_layout.xml
<LinearLayout
...
android:orientation="vertical"
>
<YourDrawer
...
/>
<FrameLayout
android:id="@+id/act_content"
...
>
// Here will be all activity content placed
</FrameLayout>
</LinearLayout>
これは、act_contentフレームに他のすべてのレイアウトを含めるための基本レイアウトになります。次に、基本アクティビティクラスを作成し、次の手順を実行します。
public abstract class DrawerActivity extends Activity {
protected LinearLayout fullLayout;
protected FrameLayout actContent;
@Override
public void setContentView(final int layoutResID) {
// Your base layout here
fullLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.act_layout, null);
actContent= (FrameLayout) fullLayout.findViewById(R.id.act_content);
// Setting the content of layout your provided to the act_content frame
getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(fullLayout);
// here you can get your drawer buttons and define how they
// should behave and what must they do, so you won't be
// needing to repeat it in every activity class
}
}
基本的に、setContentView(int resId)へのすべての呼び出しをインターセプトし、xmlからドロワーのレイアウトを拡張し、アクティビティのレイアウトを拡張し(メソッド呼び出しで提供されるreIdによって)、必要に応じてそれらを結合し、アクティビティ。
編集:
上記のものを作成したら、通常どおりアプリの作成に進み、レイアウトを作成し(ドロワーについては言及せずに)アクティビティを作成しますが、単純なアクティビティを拡張する代わりに、次のようにDrawerActivityを拡張します。
public abstract class SomeActivity extends DrawerActivity {
protected void onCreate(Bundle bundle) {
setContentView(R.layout.some_layout);
}
}
何が起こるかというと、setContentView(R.layout.some_layout)がインターセプトされます。DrawerActivityは、xmlから提供したレイアウトをロードし、ドロワーの標準レイアウトをロードし、それらを結合して、アクティビティのcontentViewとして設定します。