6

との活動がありDrawerLayoutます。ドロワーを開くには 2 つの方法があります... 画面の左側の領域から右側にスワイプする方法と、アプリのタイトルをクリックする方法です。アプリのアイコンは表示されず、タイトルのみが表示されます。ここで Google が推奨するとおりにこれを実装しました:ナビゲーション ドロワーの作成: アプリ アイコンで開いて閉じる

引き出し自体の開閉まで全て機能的です。DrawerLayoutただし、使用されると想定される標準のアイコンは表示されません。代わりに、通常の上向きキャレット (小なり記号のように見えます) を取得します。

アプリのアイコンを追加するとすぐに、ActionBar期待どおりに機能し始めます。ドロワーを開閉すると、ドロワー レイアウト アイコンが表示され、アニメーション化されます。スタイル XML ファイルとプログラムの両方でアプリ アイコンを削除しようとしました。

DrawerLayoutアプリのアイコンなしでアイコンを機能させる方法はありますか???

更新:回避策を見つけましたが、解決策というよりはハックです。単純に 1x1 ピクセルの透明な PNG (blank.png) を作成し、styles.xml ファイルでアプリのアイコンとして設定しました。以下はすべての相対コードです。

スタイル.xml

<style name="MyCustomTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyCustomActionBar</item>
    <item name="android:icon">@drawable/blank</item>
</style>

<style name="MyCustomActionBar" parent="@android:style/Widget.Holo.ActionBar">
    <item name="android:displayOptions">showHome|showTitle|homeAsUp</item>
</style>

MainActivity -> onCreate()

this.navDrawerToggle = new ActionBarDrawerToggle
(
    this,
    this.navDrawerLayout,
    R.drawable.icon_nav_drawer,
    R.string.nav_drawer_open,
    R.string.nav_drawer_closed
)
{
    public void onDrawerClosed(View view) {}
    public void onDrawerOpened(View drawerView) {}
};

MainActivity -> onPostCreate()

super.onPostCreate(savedInstanceState);
this.navDrawerToggle.syncState();

MainActivity -> onResume()

this.navDrawer.setOnItemClickListener(new DrawerItemClickListener());
this.navDrawerLayout.setDrawerListener(this.navDrawerToggle);

MainActivity -> onPause()

this.navDrawer.setOnItemClickListener(null);
this.navDrawerLayout.setDrawerListener(null);

MainActivity -> onConfigurationChanged(構成 newConfig)

super.onConfigurationChanged(newConfig);
navDrawerToggle.onConfigurationChanged(newConfig);

MainActivity -> onOptionsItemSelected(メニュー項目)

if (this.navDrawerToggle.onOptionsItemSelected(item)) {return true;}
else
{
    // A bunch of item click handling happens here...

    return true;
}
4

3 に答える 3

13

これに興味があったので、DrawerLayout のサンプルで試してみたところ、うまくいきました。また、独自のドロワー アイコン ドローアブルを使用する必要があるようですが、とにかく推奨されます。このサイトからデフォルトのアセットをダウンロードして、ナビゲーション ドロワーを作成し、それぞれのドローアブル リソース フォルダーに配置できます。

これが私のために働いたものです:

ActionBar actionBar = getActionBar();

// Let's get rid of the app icon here
actionBar.setIcon(null);
actionBar.setTitle("App Name");

// Setting these 3 options allows us to show the title as well as a "Home" elements
// "Home" elements include the Up and/or Drawer icon. The DISPLAY_HOME_AS_UP will enable
// and show the Drawer icon, we'll be "replacing" the "up" button with the drawer icon below
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE 
        | ActionBar.DISPLAY_SHOW_HOME 
        | ActionBar.DISPLAY_HOME_AS_UP);

// Get a reference of the DrawerLayout
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setDrawerListener(drawerToggle);

// Setting ActionBarDrawerToggle will allow you to set the drawables for the drawer
// (this will also give you the nice/smooth animation) as well as allow you to do some
// other things depending on the events: onDrawerClosed & onDrawerOpened.
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(
          this,                   /* host Activity */
          drawerLayout,           /* DrawerLayout object */
          R.drawable.ic_drawer,   /* nav drawer image to replace 'Up' caret */
          R.string.drawer_open,   /* "open drawer" description for accessibility */
          R.string.drawer_closed  /* "close drawer" description for accessibility */
      ) {
    public void onDrawerClosed(View view) {
        actionBar.setTitle("Closed...");
        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
    }

    public void onDrawerOpened(View drawerView) {
        actionBar.setTitle("Open...");
        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
    }
};

// Set a listener to be notified of drawer events.
drawerLayout.setDrawerListener(drawerToggle);

更新: ActionBar スタイルの android:displayOptions が考慮されていないようです。代わりに actionBar.setDisplayOptions(int options) を使用してください。

于 2013-06-18T15:56:28.063 に答える
0

ドロワー トグルを設定した後、次のメソッドを呼び出す必要があります。

 mDrawerToggle.syncState();

コードは次のようになります。

 mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description for accessibility */
            R.string.drawer_close  /* "close drawer" description for accessibility */
            ) {
        public void onDrawerClosed(View view) {
            actionBar.setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }


        public void onDrawerOpened(View drawerView) {
            actionBar.setTitle("Preview Mode");
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

    mDrawerToggle.syncState();
    mDrawerLayout.setDrawerListener(mDrawerToggle);
于 2013-10-03T15:18:05.997 に答える
0

getActionBar().setIcon(android.R.color.transparent);

これは私のために働いた..... ;)

于 2015-09-17T06:45:16.503 に答える