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

时间:2018-06-03 07:14:26

标签: java android

我构建了一个带有片段和活动的导航抽屉。所有碎片都有图标,抽屉访问顺畅如黄油,但活动中没有任何内容。活动是默认的"主页",因此访问导航抽屉至关重要。通常不会调用toggle.syncState();是解决方案,但在这种情况下它会失败。

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

有问题的活动:

public class GarageActivity extends AppCompatActivity {

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

}

清单:

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

1 个答案:

答案 0 :(得分:0)

首先,您需要启用操作栏的主页按钮。然后将Hamburger图标分配给Home按钮并编写代码以打开其监听器中的抽屉。以下是步骤:

  1. 获取汉堡/菜单图标:

    在“项目”窗口中,右键单击res文件夹,然后选择“新建”&gt;矢量资产。

    选择材料图标作为资产类型,然后点击图标按钮以打开“选择图标”窗口。

    搜索&#34; 菜单&#34;并选择菜单图标(图标为3条水平线)。 单击“确定”,然后将文件重命名为&#34; ic_menu&#34;然后单击“下一步”将其导入。

  2. 启用&#34; Home&#34;操作栏中的按钮:

    在onCreate方法中添加此代码: -

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

    首先创建DrawerLayout的全局变量,以便您可以在其他方法中访问它。在onCreate中添加对该变量的引用,并在onOptionsItemSelected中使用它来打开抽屉。以下是代码:

    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);
       }
    }
    
  4. 来源Create Navigation Drawer