私は作業用のナビゲーションドロワーを持っていましたが、ツールバーを変更したところ、ナビゲーションドロワーのアイコンが機能しなくなりました。引き出しを引っ張ってから再び動作します。これは、関連するコードです:Androidナビゲーションドロワーアイコンが機能しません
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>
は今私の質問は以下のとおりです。
- なぜハンバーガーのアイコンは機能しません。初めて?
- ナビゲーション用の引き出しを引き出した後に動作が開始するのはなぜですか?
ありがとうございます。
を 'setSearchtollbar()のためにあなたのコードをポスト;'メソッドください。 – Ibrahim
レイアウト中に 'NavigationView'の' visibility'を 'gone'に設定しているので、そうしています。リンクされた複製の答えはその動作を説明していますが、基本的に解決策はその属性を削除することです - 'android:visibility =" gone "'。 –