如何加上"菜单"指示灯旁边的操作栏的程序图标?指示灯、图标、菜单、操作

2023-09-12 01:44:42 作者:我热情有限你把握时间

目前至少的Gmail和YouTube Android应用使用侧面菜单(抽屉式导航? )打开通过刷卡,或通过单击应用程序图标(home键):

At least Gmail and Youtube Android apps use a side menu (navigation drawer?) that opens by swiping, or by clicking the app icon (home button):

是在Android SDK中的现成的部分上面的截图中的菜单指示/图标? (或自定义图标,谷歌在他们的应用程序使用?)在任何情况下,什么是让你的home键,看起来像最简单的方法,也就是说,像它会打开一个菜单?

Is the menu indicator / icon in the screenshot above a ready-made part of Android SDK? (Or a custom icon Google uses in their apps?) In any case, what's the easiest way to get your home button to look like that, i.e., like it opens a menu?

targetSdkVersion 18; 的minSdkVersion 14)

最后得到它的工作。什么是缺少对我来说是1)the实际图标 2)递延呼叫 syncState() ActionBarDrawerToggle

Finally got it working. What was missing for me was 1) the actual icon and 2) deferred call to syncState() on the ActionBarDrawerToggle.

推荐答案

要创建类似的实现/在你的应用程序看起来你应该使用 ActionBarDrawerToggle 和设置您的自定义图标,指示旁边的动作条home键。例如:

To create similar implementation / look in your application you should use ActionBarDrawerToggle and set your custom icon as indicator next to ActionBar home button. For example :

import android.app.ActionBar;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;

private void setUpDrawerToggle(){
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the navigation drawer and the action bar app icon.
    mDrawerToggle = new ActionBarDrawerToggle(
            this,                             /* host Activity */
            mDrawerLayout,                    /* DrawerLayout object */
            R.drawable.ic_drawer,             /* nav drawer image to replace 'Up' caret */
            R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
            R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
    ) {
        @Override
        public void onDrawerClosed(View drawerView) {
            invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
        }
    };

    // Defer code dependent on restoration of previous instance state.
    // NB: required for the drawer indicator to show up!
    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();
        }
    });

    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

其中, R.drawable.ic_drawer 实际上是作为指标来使用的图标。你可以找到它在 Android的资产工作室的;看Navigation抽屉指示灯 。

Where R.drawable.ic_drawer is actually the icon to use as indicator. You can find it in Android Asset Studio; see Navigation Drawer Indicator.

引用

ActionBarDrawerToggle 创建导航抽屉 ActionBarDrawerToggle Creating a Navigation Drawer
 
精彩推荐
图片推荐