0

我创建了一个具有导航抽屉的应用程序,它工作正常,但唯一的问题是我看到两个工具栏。其中有Actionbartoggle按钮和其他默认值。现在我看到的教程在抽屉布局的主要内容中使用了一个工具栏,显然导航内容来自菜单。 现在的问题是我认为我们必须在Actionbardrawertoggle的构造函数中传递工具栏引用,并且在我正确传递它之后,它可以正常工作,但是我看到两个工具栏/操作栏。 基本上我的问题是,我如何获得主要操作栏上的切换按钮,而不是第二个工具栏创建?我一直在使用为什么在创建具有导航抽屉的应用程序时看到两个工具栏?

<style name="MyTheme" parent="android:Theme.Material.Light.NoActionBar"> 

它帮助我躲上动作条试过,但我不认为这是做的很好的方式。我上传我的输出和代码的截图:

<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/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <!-- Page Content --> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:background="@color/colorPrimary" 
      android:minHeight="?attr/actionBarSize" 
      app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 
    </LinearLayout> 

    <!-- Navigation View --> 
    <android.support.design.widget.NavigationView 
     android:id="@+id/navigationView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 

     app:itemTextColor="#5DADE2" 

     app:menu="@menu/drawer_items"/> 

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

Mainactivity:

import android.content.DialogInterface; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.support.v7.app.AlertDialog; 
    import android.support.v7.app.AppCompatActivity; 
    import android.view.Menu; 
    import android.view.MenuInflater; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.widget.Button; 
    import android.support.annotation.NonNull; 
    import android.support.design.widget.NavigationView; 
    import android.support.v4.view.GravityCompat; 
    import android.support.v4.widget.DrawerLayout; 
    import android.support.v7.app.ActionBarDrawerToggle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.support.v7.widget.Toolbar; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.view.Window; 
    import android.widget.Toast;  
    import java.util.HashMap;  
    import android.content.DialogInterface; 
    import android.content.Intent; 
    import android.support.v7.app.AlertDialog; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button;  
    import java.util.HashMap; 

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{ 
    SessionManager session; 

    private Toolbar mToolbar; 
    private DrawerLayout mDrawerLayout; 
    ActionBarDrawerToggle drawerToggle; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_user_area); 

     setupToolbarMenu(); 
     setupNavigationDrawerMenu();  

    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle item selection 
     switch (item.getItemId()) { 
      case R.id.lg: 

       session.logoutUser(); 
       return true; 

      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } private void setupToolbarMenu() { 

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

     mToolbar.setTitle("Home Page"); 
    } 

    private void setupNavigationDrawerMenu() { 

     NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView); 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 
     navigationView.setNavigationItemSelectedListener(this); 
     navigationView.setItemIconTintList(null); 

     drawerToggle = new ActionBarDrawerToggle(this, 
       mDrawerLayout,mToolbar, 
       R.string.drawer_open, 
       R.string.drawer_close); 

     mDrawerLayout.addDrawerListener(drawerToggle); 

     drawerToggle.syncState(); 

    } @Override // Called when Any Navigation Item is Clicked 
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { 

     closeDrawer(); 

     switch (menuItem.getItemId()) { 

      case R.id.home: 
       // Put your Item specific Code here 
       break; 

      case R.id.invoice: 
       Intent intent = new Intent(this,details.class); 
       startActivity(intent); 
       // Put your item specific Code here 
       break; 
     } 

     return true; 
    } 

    // Close the Drawer 
    private void closeDrawer() { 
     mDrawerLayout.closeDrawer(GravityCompat.START); 
    } 

    // Open the Drawer 
    private void showDrawer() { 
     mDrawerLayout.openDrawer(GravityCompat.START); 
    } 


    @Override 
    public void onBackPressed() { 
     if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) 
      closeDrawer(); 
     else{ 
     new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit") 
       .setMessage("Are you sure?") 
       .setPositiveButton("yes", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

         Intent intent = new Intent(Intent.ACTION_MAIN); 
         intent.addCategory(Intent.CATEGORY_HOME); 
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         startActivity(intent); 
         finish(); 
        } 
       }).setNegativeButton("no", null).show();} 
    }  
} 

我styles.xml文件:

<resources> 
     <!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style>  
    </resources> 

我知道这可能不是一些使用的,但这里是我的清单文件:

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

    <uses-permission android:name="android.permission.INTERNET"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

     <activity android:name=".details" /> 
     <activity android:name=".Login" 
      android:label="Login"/> 

     <activity android:name=".MainActivity" /> 
     <activity android:name=".recycler" /> 
     <activity android:name=".RegisterActivity" /> 
     <activity android:name=".SplashScreen"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity>   
    </application>  
</manifest> 

下面是我得到的输出: enter image description here

+0

请检查下面的链接:https://stackoverflow.com/questions/24307919/action-bar-drawer-toggle-custom-icon https://stackoverflow.com/questions/29558303/change-icon-of-navigation-drawer –

回答

0

尝试按照这个例子:https://developer.android.com/training/appbar/setting-up.html

在那里,你会发现这一点:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 
    Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); 
    setSupportActionBar(myToolbar); 
    } 

你缺少的东西是:setSupportActionBar(myToolbar);

+0

好的,让我试试这个。给我几分钟。 – Robin10

+0

确定如此添加android:theme =“@ style/Theme.AppCompat.Light.NoActionBar”和setSupportActionBar(mToolbar);起了一部分作用。现在,我可以在应用程序名称的主要操作栏中看到我的抽屉。 Thx :)我接受你的回答。还有一件事,现在当我点击它时,我看到了深色的溢出菜单,我怎么能在保持抽屉在主要操作栏的同时给它白色的颜色? – Robin10

+0

我还没说过你应该使用android:theme =“@ style/Theme.AppCompat.Light.NoActionBar”...使用之前的版本:Theme.AppCompat.Light.DarkActionBar –

相关问题