2016-01-23 29 views
0

你好我正在爲Android的應用程序,我添加了一個ActionBarDrawerToggle的應用程序。當我選擇一個項目時,ActionBarDrawerToggle消失。如何在整個應用程序中顯示ActionBar Android

我想做的事: 在MainWindow.java創建ActionBarDrawerToggle [完成] 創建Profil.java和Planning.java並延伸到主窗口和PROFIL規劃顯示ActionBarDrawerToggle。 [問題]

我使用:

UPDATE

Mainactivity

public class MainWindow extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

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

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     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); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.mainwindow, menu); 

     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.nav_profil) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_profil) { 
      Intent intent = new Intent(this, Profil.class); 
      startActivity(intent); 
     } else if (id == R.id.nav_planning) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

的PROFIL

public class Profil extends MainWindow { 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.content_profil); 
     System.out.println("Profil"); 
    } 
} 

AndroidManifest.xml中

 <?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.main"> 

    <!-- To auto-complete the email text field in the login form with the user's emails --> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.READ_PROFILE" /> 
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 

    <application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".Login" 
     android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".MainWindow" 
     android:label="@string/title_activity_mainwindow"></activity> 
    <activity 
     android:name=".Connection"></activity> 
    <activity 
     android:name=".Profil"></activity> 
</application> 

</manifest> 
+0

'Profil'具有'NoActionBar'主題,所以除非設置一個主題,否則不會有'ActionBar'。 –

+0

謝謝,我刪除它,但它不顯示與菜單的操作欄 我正在使用這個:https://raw.githubusercontent.com/xiprox/Material-Action-Bar-Sample/master/screenshots /1.png 並刪除後顯示此(沒有圖標):http://www.jayway.com/wp-content/uploads/2014/06/intended-result1.png –

+0

'Profil'也有重寫'onCreateOptionsMenu()'方法來獲得菜單。 –

回答

1

,如果你使用的是物質上的支持lib中,你將不得不手動添加操作欄的XML文件

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="@dimen/appbar_padding_top" 
    android:theme="@style/AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/PopupOverlay"/> 

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

,並在的onCreate java的活動文件()方法中添加以下行

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 

希望這對你的作品

+0

我試過這個謝謝,但它沒有工作^^' –

+0

好吧,我發現這只是刪除行setContentView(R.layout.content_profil);形成你的Profil類,看看 –

+0

我需要這個佈局來顯示信息 –

0

請確保@ style/AppTheme主題不是NoActionBar。

相關問題