Androidには非常に新しいので、Androidでレイアウトを使用する標準的な方法についていくつかの混乱があります。基本的に、私はiOS開発者です。 NavigationDrawerViewテンプレートを使用して新しいプロジェクトを作成しました。今度は、ナビゲーションビューでツールバーと引き出しを使って別のアクティビティを作成する必要があります。基本的には、ツールバー、引き出し、ナビゲーションビューを持つレイアウトを設計したいだけです。これはアクティビティに含まれるだけで、コンテンツは新しく作成されたアクティビティで設計できます。誰もがこれを行うための最良の方法を提案することができます。私は、すべての人がナビゲーション・ドロワー・ビューのデフォルト・テンプレート設計を知っていることを願っています。だから私はここにコードを含めていない。誰かがコードを見たいと思ったら、私に知らせてください。あなたは次のXMLファイルを使用する必要がありそのために引き出しとナビゲーションビューでツールバーのレイアウトを再利用
-3
A
答えて
1
: content_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.android.MainActivity"//you main activity
tools:showIn="@layout/app_bar_main">
</RelativeLayout>
app_bar_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/actionbar"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</RelativeLayout>
activity_main:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/navDrawerbg"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
そしてメインのjavaを作りますファイルie mainactivity extends AppcompatActivity and implements NavigationView.OnNavigationItemSelectedListener
グローバルvarible宣言:
private Context mContext;
private static FragmentManager mManager;
Fragment fragment = null;
はOnCreateの後に方法の下に作成し
プライベートボイドinitUI(){
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_main, fragment).commit();
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Home");
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
fragmentviewをIntiate
private void initiateFragmentView() {
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_main, fragment).commit();
}
}
そして(setcontentview後のOnCreateメソッドでメソッドの上に呼び出す)
mManager = getSupportFragmentManager();
fragment = new Home();
mContext = this;
initUI();
引き出しは、あなたが、フラグメントを使用する主な活動でフレームを使用しての断片を切り替えることができます
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
//Your fragment
fragment = new Home();
initiateFragmentView();
} else if (id == R.id.nav_xyz) {
fragment = new xyz();
initiateFragmentView();
} else if (id == R.id.nav_abc) {
fragment = new abc();
initiateFragmentView();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
関連する問題
- 1. 再度ツールバーと下部のナビゲーションビューを表示
- 2. Xamarin.Forms - C#レイアウトを再利用
- 3. アクションバーの引き出しレイアウト
- 4. Androidナビゲーションビューの引き出しが閉じない
- 5. 引き出しレイアウトでの遷移
- 6. スクロールでツールバーと引き出しを隠す
- 7. AppWidgetの場合のレイアウトの再利用
- 8. ナビゲーションビューは、以下のアプリバーのレイアウトと
- 9. 子レイアウトを再利用できますか?
- 10. Recycleviewer - レイアウトを再利用するには?
- 11. アンドロイドのツールバーの下にあるナビゲーションビュー
- 12. Codeigniter再利用可能なレイアウト?
- 13. Androidコードとレイアウトを再利用するためのベストプラクティス
- 14. XamarinのAndroidで同時に引き出しレイアウトとタブレイアウトを使用する
- 15. Android:複数のレイアウトを扱うときにcustomadapterを再利用する
- 16. ポリマーアプリ-引き出しレイアウトresponsiveWidth属性
- 17. NITimerの問題を引き起こすUITableViewセルの再利用
- 18. 2つの異なるレイアウトでAndroid Listviewのビューを再利用
- 19. 引き出しレイアウトのツールバーを非表示にするにはどうすれば引き出し引き出しボタン(3行)を使用することができますか?
- 20. CDECL呼び出し規約では、スタックにプッシュした引数を再利用できますか?
- 21. ExpressionEngineでコードブロックを再利用して引数を渡す方法
- 22. 引き出しのレイアウトXamarin.Android.Support.Core.UIとXamarin.Android.Support.V4の両方に存在
- 23. 同じレイアウトでイメージを再利用する
- 24. <include/>でレイアウトを再利用する方法は?
- 25. Appcompat 23.2.1ナビゲーションバーをステータスバーでカバーした引き出しレイアウト
- 26. XMLレイアウトのView要素のID文字列を再利用
- 27. コーディネーターのレイアウトのツールバー
- 28. ツールバーがあるときに断片化したアンドロイドのレイアウト
- 29. グローバルnpmインストール後に引き出しがまだ利用できない
- 30. Android - 折りたたみツールバーのレイアウトでツールバーを修正できません
をitemselectedとき、あなたの破片を呼び出します要件を満たすために単一のアクティビティにあり、フラグメント間を切り替える必要があります。すべてのビューに簡単にアクセスできます –