ñ我的主要活動我有一個導航抽屜,並通過點擊它我加載不同的片段,如首頁,購物車,設置等。最初在我的主要活動,我得到了工具欄與微調並按鈕導航抽屜以及。但對於其他片段,我需要根據需要自定義工具欄。編輯工具欄根據片段顯示
這裏是我的activity_main.xml中如何實現的:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigation_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="@bool/fitsSystemWindows">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_kitkat_height"
android:background="?colorPrimary" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_lollipop_height"
android:background="?colorPrimaryDark" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/status_bar_margin_top">
<FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?actionBarSize" />
<!--include different toolbars into the activity dont know this is correct implementation... If I missed any of them It gives me a null pointer exception in MainActivity..-->
<include
android:id="@+id/toolbar_cart"
layout="@layout/toolbar_cart" />
<include
android:id="@+id/toolbar_home"
layout="@layout/toolbar_home" />
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="@bool/fitsSystemWindows"
app:headerLayout="@layout/navigation_drawer_header"
app:menu="@menu/navigation_drawer_menu"
app:theme="@style/NavigationViewTheme" />
</android.support.v4.widget.DrawerLayout>
然後,我實現的方法根據片段加載自定義工具欄(主要活動內):
public void customizeToolBar(int position) {
switch (position) {
case 0:
toolbar = (Toolbar) findViewById(R.id.toolbar_home);
setSupportActionBar(toolbar);
subCategories_spinner = (Spinner) findViewById(R.id.spinner_subCategories);
actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
break;
case 1:
toolbar = (Toolbar) findViewById(R.id.toolbar_cart);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setTitle("Check Out");
actionBar.setDisplayHomeAsUpEnabled(true);
break;
....
然後我在下面的NavigationItemSelectedListener中調用該方法:
private void setupNavigationDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.item_navigation_drawer_home:
menuItem.setChecked(true);
customizeToolBar(0);
setFragment(0);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.item_navigation_drawer_cart:
menuItem.setChecked(true);
customizeToolBar(1);
setFragment(1);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
...
但這不起作用。每次顯示相同的工具欄。我新來android。任何建議,使這項工作將不勝感激。謝謝。
很好的答案Hoang Nguyen。我用你的第一個解決方案做到了。我不知道'toolbar.setVisibility(View.VISIBLE);'。我只是Android開發的初學者。我之前正在嘗試使用第二種解決方案。但是,如果我沒有在main_activity中包含工具欄,它總是會給我一個空指針異常。一旦我包含了,我的顯示器中會出現多個工具欄。無論如何非常感謝解決方案。 :) –
沒問題。使用第二種解決方案,它會得到您在主要活動中包含的工具欄,您可以像這樣更改視圖: http://stackoverflow.com/questions/3334048/android-layout-replacing-a-view-with-another -view-on-run-time 對不起,我忘了它。 –