简体   繁体   English

活动中缺少导航抽屉汉堡包图标

[英]Missing Navigation Drawer hamburger icon in Activity

I built a Navigation Drawer with fragments and an Activity. 我用片段和活动构建了一个导航抽屉。 All the fragments have the icon and drawer access is smooth as butter, but there is nothing in the Activity. 所有片段都有图标,抽屉访问就像黄油一样顺畅,但是活动中没有任何内容。 The Activity is the default "Home Page", so access to the Navigation Drawer is critical. 该活动是默认的“主页”,因此访问导航抽屉至关重要。 Typically not calling toggle.syncState(); 通常不调用toggle.syncState(); is the solution, but it fails in this case. 是解决方案,但在这种情况下会失败。

MainActivity: 主要活动:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

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

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

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.setDrawerIndicatorEnabled(true);
    toggle.syncState();

    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //Sets default fragment
    Intent i = new Intent(MainActivity.this,GarageActivity.class);
    startActivity(i);

    navigationView.setCheckedItem(R.id.nav_garage);
}

//Name in Action bar
public void setActionBarTitle(String title) {
    getSupportActionBar().setTitle(title);
}

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

Activity in question: 有问题的活动:

public class GarageActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_garage);
    getSupportActionBar().setTitle("My Garage");
}

} }

Manifest: 表现:

<?xml version="1.0" encoding="utf-8"?>

<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=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".GarageActivity">
        <meta-data
            android:name="android.support.parent_activity"
            android:value=".MainActivity"/>
    </activity>
</application>

First you need to enable actionbar's Home button. 首先,您需要启用操作栏的“ 主页”按钮。 Then Assign Hamburger icon to Home button and write code to open the drawer in his listener. 然后将“汉堡包”图标分配给“主页”按钮,并编写代码以在其侦听器中打开抽屉。 following are the steps: 步骤如下:

  1. Get hamburger/menu icon: 获取汉堡包/菜单图标:

    In the Project window, right-click the res folder and select New > Vector Asset. 在“项目”窗口中,右键单击res文件夹,然后选择“新建”>“矢量资产”。

    Select Material icon as the asset type and then click the Icon button to open the Select Icon window. 选择“ 材料”图标作为资产类型,然后单击“ 图标”按钮以打开“选择图标”窗口。

    Search for " menu " and select the menu icon (the icon is 3 horizontal lines). 搜索“ 菜单 ”并选择菜单图标(该图标为3条水平线)。 Click OK, and then rename the file to "ic_menu" and click Next to import it. 单击“确定”,然后将文件重命名为“ ic_menu”,然后单击“下一步”将其导入。

  2. Enable "Home" button in actionbar: 在操作栏中启用“主页”按钮:

    add this code in your onCreate method:- 将此代码添加到您的onCreate方法中:

     ActionBar actionbar = getSupportActionBar(); actionbar.setDisplayHomeAsUpEnabled(true); actionbar.setHomeAsUpIndicator(R.drawable.ic_menu); 
  3. Add code in onOptionsItemSelected method: onOptionsItemSelected方法中添加代码:

    first create global variable of DrawerLayout so you can access it in other methods. 首先创建DrawerLayout的全局变量,以便您可以通过其他方法访问它。 add reference to that variable in onCreate and use it in onOptionsItemSelected to open the drawer. 在onCreate中添加对该变量的引用,并在onOptionsItemSelected中使用它来打开抽屉。 following is the code: 以下是代码:

     public class MainActivity extends AppCompatActivity { private DrawerLayout mDrawerLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDrawerLayout = findViewById(R.id.drawer_layout); ... } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: mDrawerLayout.openDrawer(GravityCompat.START); return true; } return super.onOptionsItemSelected(item); } } 

source Create Navigation Drawer 创建导航抽屉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM