[英]android - open navigation drawer by clicking the app icon?
我在我的應用程序中使用導航抽屜(通過從屏幕左側滑動打開的導航類型)。 現在,通常您也可以通過點擊應用程序圖標打開它,當我查找它時,我發現了一大堆代碼要添加到您的活動中。 只是為了一個簡單的按鈕。 我想那不是我想要的東西嗎? 如果真的需要為單個按鈕設置如此多的代碼,那么使代碼更清晰的最佳方法是什么? 謝謝你,抱歉是這樣一個Android新手。
我也遇到了這個問題,假設你已經有@Bernald建議的ActionBarDrawerToggle,你還需要在你的Activity中添加以下內容:
@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);
}
這是讓切換按下ActionBar上的圖標按鈕,導致抽屜滑出。
您需要的一切都在Google的“ 導航抽屜指南”中進行了描述。 基本上,您需要在ActionBar上啟用“up”操作:
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
然后你需要將它綁定到Toggle:
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.