1

我打開MyActivity時默認顯示Home(漢堡)按鈕。不幸的是我在左上角沒有看到任何按鈕。但是當我打開並關閉我的抽屜後,出現Home按鈕。AppCompat v7-r23。工具欄中的主頁按鈕默認不顯示

我用的是最新的程序兼容性-V7:23.0.1庫:

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:23.0.1' 
} 

我的活動:

public class MyActivity extends AppCompatActivity{ 

    private DrawerLayout mDrawerLayout; 
    private ActionBarDrawerToggle mDrawerToggle; 

    private CharSequence mTitle; 

    private Toolbar toolbar; 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_drawer); 

     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     if (toolbar != null) { 
      setSupportActionBar(toolbar); 

      getSupportActionBar().setHomeButtonEnabled(true); 
     } 

     mTitle = getTitle(); 

     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerToggle = new ActionBarDrawerToggle(
       this, 
       mDrawerLayout, 
       toolbar, 
       R.string.hello_world, 
       R.string.app_name) 
     { 
      public void onDrawerClosed(View view) { 
       super.onDrawerClosed(view); 
       toolbar.setTitle(mTitle); 
       invalidateOptionsMenu(); 
       syncState(); 
      } 

      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       //toolbar.setTitle(mDrawerTitle); 
       invalidateOptionsMenu(); 
       syncState(); 
      } 
     }; 

     mDrawerLayout.setDrawerListener(mDrawerToggle); 
    } 
} 

activity_drawer.xml:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    style="@style/MatchParent"> 

    <!-- Your normal content view --> 
    <LinearLayout 
     style="@style/MatchParent" 
     android:orientation="vertical"> 

     <include layout="@layout/toolbar"/> 

     <FrameLayout 
      android:id="@+id/fragment_container" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

    </LinearLayout> 

    <FrameLayout 
     android:layout_width="304dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="left|start"> 

     <include layout="@layout/navigation_drawer" /> 

    </FrameLayout> 

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

我已經試過了以下變體默認顯示主頁按鈕:

getSupportActionBar().setDisplayShowHomeEnabled(true); 
getSupportActionBar().setHomeButtonEnabled(true); 

但它不起作用。

例如,如果我只設置:

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

然後向上(箭頭)按鈕正確地顯示,當我打開活動,並轉化爲Home鍵後,我打開和關閉我的抽屜裏。

可能是什麼問題?

在此先感謝。

回答

1

如果你不調用syncState您的活動的onPostCreate或不通過打電話向onConfigurationChangedonOptionsItemSelected相當於你的Activity回調,你應該做的。

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    // Sync the toggle state after onRestoreInstanceState has occurred. 
    mDrawerToggle.syncState(); 
} 

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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Pass the event to ActionBarDrawerToggle, if it returns 
    // true, then it has handled the app icon touch event 
    if (mDrawerToggle.onOptionsItemSelected(item)) { 
     return true; 
    } 
    // Handle your other action bar items... 

    return super.onOptionsItemSelected(item); 
} 
相關問題