gpt4 book ai didi

java - Android 抽屉导航图标不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 11:26:35 33 4
gpt4 key购买 nike

我有一个可用的抽屉导航,但我更改了工具栏,现在抽屉导航的图标停止工作。拉出抽屉后又可以使用了。这是相关代码:

MainActivity.java

//Drawer and fragments variables
private FragmentManager fragmentManager;
private ActionBarDrawerToggle toggle;
private DrawerLayout drawer;

//Searchbar variables
private Toolbar toolbar, searchtollbar;
private Menu search_menu;
private MenuItem item_search;

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

//Setup toolbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setSearchtollbar();

//Setup navigationdrawer
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.getMenu().getItem(0).setChecked(true);

//Setup fragments
fragmentManager = getSupportFragmentManager();
try{
fragmentManager.beginTransaction().replace(R.id.container, HomeFragment.class.newInstance()).commit();
}catch(Exception e){
handleError(e);
}
}

@Override
public void onBackPressed() {
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) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
toggle.syncState();
}

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

@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.
//noinspection SimplifiableIfStatement
Log.i("TAG", "Item clicked: "+item.getItemId());
if (toggle.onOptionsItemSelected(item)) {
return true;
}
switch(item.getItemId()){
case R.id.action_settings:{
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
}
}
return super.onOptionsItemSelected(item);
}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Log.i("TAG", "Navigation item clicked: " + id);
Class fragmentClass;
Fragment fragment = null;

switch(id) {
...
}

try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
handleError(e);
}

fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

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

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/app_bar_main"/>
<include layout="@layout/app_bar_main_search"
android:visibility="invisible"/>
<include
layout="@layout/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"/>
</RelativeLayout>

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:visibility="gone"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />

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

现在我的问题是:

  • 为什么汉堡包图标第一次不起作用?
  • 为什么拉出抽屉导航后它就开始工作?

提前致谢。

最佳答案

使用此代码:

 ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(                this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);toggle.setDrawerIndicatorEnabled(false); //disable "hamburger to arrow" drawable        toggle.setHomeAsUpIndicator(R.drawable.menu); //set your own        // setting click listener on hamburger icon        toolbar.setNavigationOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                mDrawerLayout.openDrawer(Gravity.START);                }            }        });        // setting listerner on drawer        mDrawerLayout.addDrawerListener(toggle);        toggle.syncState();

关于java - Android 抽屉导航图标不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44292299/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com