簡體   English   中英

通過操作欄標題切換導航抽屜

[英]Toggle the Navigation Drawer via the Action Bar title

我試圖通過點擊操作欄標題(這是當前Android Gmail應用程序的設置方式)允許用戶在我的應用中打開/關閉導航抽屜。 此時,用戶可以通過點擊應用程序/抽屜圖標或通過左右滑動將其滑入來切換抽屜。 但是,操作欄標題本身不可單擊。 根據開發人員文檔 ,當我們使用NAVIGATION_MODE_STANDARD時,單擊操作欄標題應該“使用具有項目ID android.R.id.home的MenuItem將onOptionsItemSelected調度到主機Activity”但由於某種原因我無法獲得行為標題這條路。

我相信導航抽屜本身很好,但這是我如何設置動作欄:

private void configureActionBar(CharSequence mTitle) {

    ActionBar actionBar = getActionBar();

    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

    actionBar.setIcon(R.drawable.ic_blank);

    GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                new int[] {
                0xFF004700, 0xFF002900
                });

    gd.setCornerRadius(0f);

    actionBar.setBackgroundDrawable(gd);

    // set the title of the action bar using the given title
    actionBar.setTitle(mTitle);

}

任何建議將不勝感激!

如果你想通過點擊ActionBar的Icon / Title來打開抽屜,我建議你使用支持庫中提供的ActionBarDrawerToggle類( android.support.v4.app.ActionBarDrawerToggle

參考: https//developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html

使用示例:
https://developer.android.com/training/implementing-navigation/nav-drawer.html

當捕獲onOptionsItemSelected()中的事件時,必須將其傳遞給ActionBarDrawerToggle,以便它可以處理打開/關閉抽屜請求:

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

如果在AndroidManifest.xml中設置應用程序主題屬性,則顯示圖標/標題,如下所示:

    <application
    android:name=".SampleApp"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

res / values / styles.xml包含主題聲明

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

它的工作原理是使用android.support.v7.app.ActionBarDrawerToggle,同時不推薦使用support.v4類。 請參見如何使用support-v7-appcompat庫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM