我是android開發新手。我已經建立了我的應用程序,無需導航抽屜。現在,我想添加導航抽屜,它將在我的應用中的所有活動中保持一致。在這個問題上幫助我。如何在預建應用程序上添加導航抽屜?
0
A
回答
0
假設您的原始活動擴展了'AppCompatActivity',請將基本活動設置爲如下並將其他所有活動擴展到'BaseActivity'。 BaseActivity佈局將內容DrawaerLayout和一個framelayout。在BaseActivity中,使用setContentview方法,並在framelayout中填充活動佈局。
BaseActivity.Java
public class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private FrameLayout baseLayout;
public ActionBarDrawerToggle drawerToggle;
public Toolbar toolbar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.base_layout);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
baseLayout = (FrameLayout) findViewById(R.id.base_view);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView.setNavigationItemSelectedListener(this);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, 0, 0);
drawerLayout.addDrawerListener(drawerToggle);
}
@Override
public void setContentView(View view) {
if (baseLayout != null) {
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
baseLayout.addView(view, params);
}
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
if (baseLayout != null) {
baseLayout.addView(view, params);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//TODO
return false;
}
}
base_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
style="@style/Widget.MyApp.Toolbar.Solid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/base_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="280sp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/base_header"
app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>
base_header.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/blank"
android:orientation="vertical">
<ImageView
android:id="@+id/nav_header_icon"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_marginBottom="8dp"
android:layout_marginStart="16sp"
android:contentDescription="@null"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toTopOf="@+id/nav_header_title"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/nav_header_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16sp"
android:layout_marginStart="16sp"
android:fontFamily="sans-serif-medium"
android:text="@string/app_name"
app:layout_constraintBottom_toTopOf="@+id/nav_header_subtitle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/nav_header_subtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16sp"
android:layout_marginStart="16sp"
android:fontFamily="sans-serif"
android:text="@string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
在 「菜單中的」 添加文件夾drawer.xml在你的資源。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/icon_home"
android:title="Home" />
</menu>
相關問題
- 1. 在片段上添加導航抽屜
- 2. 在導航抽屜上添加EditText Android
- 3. 如何在使用導航抽屜的片段時在底部導航欄上添加導航抽屜?
- 4. 添加導航抽屜AppCompatActivity
- 5. Web應用程序的導航抽屜
- 6. 如何使用反應導航在導航抽屜中添加節分隔線
- 7. 在導航中添加圖標抽屜
- 8. 在片段上創建導航抽屜
- 9. 添加按鈕,導航抽屜片段
- 10. 添加項目到導航抽屜
- 11. 嚮導航抽屜添加switch語句
- 12. 動態添加MDL導航抽屜
- 13. 將標題添加到導航抽屜
- 14. 將圖標添加到導航抽屜
- 15. Android添加頁腳到導航抽屜
- 16. 導航抽屜如何添加上述列表視圖
- 17. 導航抽屜
- 18. 創建導航抽屜
- 19. 如何構建導航抽屜?
- 20. Android Studio導航抽屜,如Gmail應用
- 21. 將導航抽屜添加到Android Studio中的我的應用程序
- 22. 無法在應用程序啓動時關閉導航抽屜
- 23. 導航抽屜:fragmentTransaction.commit()正在崩潰我的應用程序
- 24. 導航抽屜在應用程序啓動時打開
- 25. 如何使用ArrayAdapter添加項目到導航抽屜活動
- 26. 如何在所有活動中添加導航抽屜
- 27. 如何在導航抽屜活動中添加Google地圖?
- 28. 如何在導航抽屜中添加頁腳?
- 29. 如何在導航抽屜中添加自定義ListView?謝謝
- 30. 如何在抽屜式導航圖標添加到項目
最簡單的方法將是使用帶有NavigationDrawer只有一個活動並使用不同的內容片斷(而不是有許多不同的活動的老辦法)... – deHaar