2017-03-02 115 views
0

我目前正在創建一個不使用片段的導航菜單。我似乎創建了一個工作系統,它打開類,並通過使用BaseActivity並將它繼承到需要抽屜的類中,從而使導航欄保持在每個階段。然而,在加載應用程序時,看起來3杆'漢堡'圖標不存在,頂部的工具欄也不存在(見圖)。導航欄似乎有用,因爲我可以從左側滑動並點擊加載其他課程。我覺得我的onCreate()在我的BaseActivity是缺少的東西,但用谷歌搜索我似乎無法找到解決方案。抽屜佈局漢堡包和工具欄不可見,儘管工作導航抽屜?

圖片當前問題:導航欄的

Image of current issue

圖像的問題,在後臺工作:

Image of Nav Bar Working with issue in background

這是我的基本活動:

import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.NavigationView; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.MenuItem; 

public class BaseActivity extends AppCompatActivity { 

    DrawerLayout drawerLayout; 
    ActionBarDrawerToggle actionBarDrawerToggle; 
    Toolbar toolbar; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view); 

     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_closed); 
     drawerLayout.addDrawerListener(actionBarDrawerToggle); 


     navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
      @Override 
      public boolean onNavigationItemSelected(MenuItem item) { 
       Intent anIntent; 
       switch (item.getItemId()) { 

        case R.id.nav_homepage: 
         anIntent = new Intent(getApplicationContext(), homepage.class); 
         startActivity(anIntent); 
         drawerLayout.closeDrawers(); 
         break; 
        case R.id.nav_add_roster: 
         anIntent = new Intent(getApplicationContext(), add_roster.class); 
         startActivity(anIntent); 
         drawerLayout.closeDrawers(); 
         break; 
        case R.id.nav_check_schedule: 
         anIntent = new Intent(getApplicationContext(), schedule.class); 
         startActivity(anIntent); 
         drawerLayout.closeDrawers(); 
         break; 
       } 
       return false; 
      } 
     }); 
    } 
} 

這是我的homepag Ë這就是最初加載:

import android.os.Bundle; 
import android.widget.FrameLayout; 

public class homepage extends BaseActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame); //Remember this is the FrameLayout area within your content_main.xml 
     getLayoutInflater().inflate(R.layout.homepage, contentFrameLayout); 
    } 
} 

這裏是我的活動主:

<?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"> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/content_frame"/> 

    </LinearLayout> 

    <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="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

回答

0

你可以試試這個,你BaseActivity添加如下代碼:

@Override 
    public void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     actionBarDrawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     actionBarDrawerToggle.onConfigurationChanged(newConfig); 
    } 

編輯:

在BaseActivity的onCreate函數中也啓用主頁按鈕:

getSupportActionBar().setHomeButtonEnabled(true); 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

有丟失在XML工具欄佈局:

<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"> 

    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/rootLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#eeeeee"> 

     <android.support.design.widget.AppBarLayout 
      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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

     </android.support.design.widget.AppBarLayout> 

     <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

      <FrameLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/content_frame"/> 

     </LinearLayout> 

    </android.support.design.widget.CoordinatorLayout> 

    <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="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 
</android.support.v4.widget.DrawerLayout> 

希望這有助於;

對不起,我的英語。

+0

試過這個在我的谷歌搜索,沒有運氣恐怕 –

+0

看看我的編輯是否幫助你。 – Cochi

+0

Nada,強制關閉空指針。我正在使用NoActionBar主題,所以這可能是原因... java.lang.NullPointerException:嘗試調用虛擬方法'void android.support.v7.app.ActionBar.setHomeButtonEnabled(boolean)'on a null對象引用 –

0

嘗試這種方式,

<?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"> 


<include 
    layout="@layout/app_bar_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

    <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="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

和app_bar_toolbar.xml,

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 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="match_parent"> 


    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <RelativeLayout 
      android:id="@+id/relative_tool_bar" 
      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.support.v7.widget.Toolbar> 
     </RelativeLayout> 
    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_main" /> 
</android.support.design.widget.CoordinatorLayout> 

和content_main.xml,使用此。

<?xml version="1.0" encoding="utf-8"?> 
<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="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.esec_0052.fingertips.FingerTipMainActivity" 
    tools:showIn="@layout/app_bar_finger_tip_main"> 


    <FrameLayout 
     android:id="@+id/frame_parent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</RelativeLayout>