由於我對Android非常陌生,因此我對Android中使用佈局的標準方式感到困惑。基本上,我是一名iOS開發人員。我已經使用NavigationDrawerView模板創建了一個新項目。現在我必須用導航視圖的工具欄和抽屜創建另一個活動。基本上,我想設計一個具有工具欄,抽屜和導航視圖的佈局,它可以包含在一個活動中,並且內容可以在新創建的活動中進行設計。任何人都可以提出最好的方法來做到這一點。我希望每個人都知道導航抽屜視圖的默認模板設計。所以我不在這裏包含代碼。如果有人想看代碼,請告訴我。重複使用抽屜和導航視圖的工具欄佈局
-3
A
回答
1
對於您必須使用以下XML文件: 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文件即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);
}
Intiate的fragmentview
private void initiateFragmentView() {
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_main, fragment).commit();
}
}
而且在onCreate方法調用上述方法的setContentView()
mManager = getSupportFragmentManager();
fragment = new Home();
mContext = this;
initUI();
後打電話給您的片段時,抽屜itemselected
@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. Android抽屜導航工具欄丟失
- 2. 抽屜佈局漢堡包和工具欄不可見,儘管工作導航抽屜?
- 3. 導航抽屜使用Tablayout和摺疊工具欄
- 4. 在片段中使用導航抽屜和工具欄
- 5. 抽屜CoordinatorLayout和工具欄
- 6. 展開導航抽屜的動作欄/工具欄
- 7. 安卓工具欄圖像居中與導航抽屜按鈕
- 8. 如何使片段覆蓋工具欄中的導航抽屜
- 9. 導航抽屜狀態欄
- 10. 獲取導航抽屜滑動工具欄/操作欄下方
- 11. 用於Android Studio的導航抽屜主題的透明工具欄,帶有圖像重疊工具欄
- 12. 抽屜式導航欄圖標顏色
- 13. 具有框架佈局的導航抽屜
- 14. 工具欄 - 重力無法找到抽屜式視圖LEFT
- 15. 安卓導航抽屜上的自定義工具欄
- 16. 如何刪除導航抽屜工具欄中的三個點
- 17. 帶導航抽屜的透明工具欄
- 18. Android導航抽屜錯誤的工具欄標題onResume
- 19. 如何在Android中的工具欄下設置導航抽屜?
- 20. 在導航抽屜佈局中顯示網格佈局?
- 21. 將導航抽屜鏈接到工具欄按鈕
- 22. ImageView不會出現在導航抽屜工具欄
- 23. 中心工具欄標題自定義工具欄中的圖標和導航抽屜切換按鈕
- 24. 導航抽屜不工作
- 25. 如何隱藏抽屜佈局的工具欄,仍然可以使用打開的抽屜按鈕(三行)?
- 26. 如何在使用導航抽屜的片段時在底部導航欄上添加導航抽屜?
- 27. Android導航抽屜圖標不工作
- 28. 隱藏工具欄和抽屜滾動
- 29. Android谷歌地圖與導航抽屜佈局
- 30. 我可以在導航抽屜佈局添加圖像頭
可以使用片段,用片段之間的主要活動框架和交換機您的需求,以便您可以在單個活動中根據需要在片段之間切換。每個視圖都很容易訪問 –