导航抽屉不使用R.drawable.ic_drawer或滑动关闭

时间:2013-11-22 20:55:05

标签: java android android-drawable navigation-drawer

我刚刚在我的应用中添加了导航抽屉,但好像R.drawable.ic_drawerR.drawable.drawer_shadow似乎没有被使用(它只是在ic_drawer中显示黑色背景而不是灰色背景) 。

我遇到的另一个问题是导航抽屉将打开 - 但除非我按下后退按钮,否则似乎没有关闭。

我使用了以下示例:

http://hmkcode.com/android-creating-a-navigation-drawer/

但是现在我正试图弄清楚如何修改以下内容以便在从右向左滑动时关闭它。

截图:

enter image description here

JAVA:

    public class Home extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener,
YouTubeThumbnailView.OnInitializedListener {

    public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    public static final String VIDEO_ID = "o7VVHhK9zf0";
    public static final String VIDEO1_ID = "xVHHJqntuXI";
    public static final String VIDEO2_ID = "YWteQj_q3Ro";
    public static final String VIDEO3_ID = "83ZgtqTw-mI";
    public static final String VIDEO4_ID = "n5wMza29JwI";
    private YouTubePlayer youTubePlayer;
    private YouTubePlayerView youTubePlayerView;
    private YouTubeThumbnailView youTubeThumbnailView1;
    private YouTubeThumbnailView youTubeThumbnailView2;
    private YouTubeThumbnailView youTubeThumbnailView3;
    private YouTubeThumbnailView youTubeThumbnailView4;
    private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private DrawerLayout mDrawerLayout;
    private ListView drawerListView;
    private ListView mDrawerList;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private ActionBarDrawerToggle mDrawerToggle;

    private YouTubeThumbnailLoader youTubeThumbnailLoader;
    ScrollView mainScrollView;
    Button fav_up_btn1;
    Button fav_dwn_btn1;
    ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);


        final ActionBar actionBar = getActionBar();
        actionBar.setCustomView(R.layout.actionbar_custom_view_home);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);

        // get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.items);

        // get ListView defined in activity_main.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_listview_item, drawerListViewItems));

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
                drawerLayout, /* 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 */
                );
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);

        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
        youTubePlayerView.initialize(API_KEY, this);

        mainScrollView = (ScrollView) findViewById(R.id.groupScrollView);

        youTubeThumbnailView1 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview1);
        youTubeThumbnailView1.initialize(API_KEY, this);

        fav_up_btn1 = (Button) findViewById(R.id.fav_up_btn1);

        fav_up_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean favIsUp = fav_up_btn1
                        .getBackground()
                        .getConstantState()
                        .equals(getResources().getDrawable(
                                R.drawable.fav_up_btn1).getConstantState());
                // set the background
                fav_up_btn1
                .setBackgroundResource(favIsUp ? R.drawable.fav_dwn_btn1
                        : R.drawable.fav_up_btn1);
            }
        });

        youTubeThumbnailView1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (youTubePlayer != null) {
                    youTubePlayer.cueVideo(VIDEO1_ID);
                    youTubePlayer.play();
                    mainScrollView.smoothScrollTo(0, 0);
                }
            }
        });

        youTubeThumbnailView2 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview2);
        youTubeThumbnailView2.initialize(API_KEY, this);

        youTubeThumbnailView2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg1) {
                if (youTubePlayer != null) {
                    youTubePlayer.cueVideo(VIDEO2_ID);
                    youTubePlayer.play();
                    mainScrollView.smoothScrollTo(0, 0);
                }
            }
        });

        youTubeThumbnailView3 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview3);
        youTubeThumbnailView3.initialize(API_KEY, this);

        youTubeThumbnailView3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg1) {
                if (youTubePlayer != null) {
                    youTubePlayer.cueVideo(VIDEO3_ID);
                    youTubePlayer.play();
                    mainScrollView.smoothScrollTo(0, 0);
                }
            }
        });

        youTubeThumbnailView4 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview4);
        youTubeThumbnailView4.initialize(API_KEY, this);

        youTubeThumbnailView4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg1) {
                if (youTubePlayer != null) {
                    youTubePlayer.cueVideo(VIDEO4_ID);
                    youTubePlayer.play();
                    mainScrollView.smoothScrollTo(0, 0);
                }
            }

        });

    }

    @Override
    public void onInitializationFailure(Provider provider,
            YouTubeInitializationResult result) {

    }

    @Override
    public void onInitializationSuccess(Provider provider,
            YouTubePlayer player, boolean wasRestored) {

        youTubePlayer = player;

        if (!wasRestored) {
            player.cueVideo(VIDEO_ID);

        }
    }

    @Override
    public void onInitializationFailure(YouTubeThumbnailView thumbnailView,
            YouTubeInitializationResult error) {

    }

    @Override
    public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
            YouTubeThumbnailLoader thumbnailLoader) {

        youTubeThumbnailLoader = thumbnailLoader;
        thumbnailLoader
        .setOnThumbnailLoadedListener(new ThumbnailLoadedListener());

        youTubeThumbnailLoader.setVideo(VIDEO_ID);

    }

    private final class ThumbnailLoadedListener implements
    YouTubeThumbnailLoader.OnThumbnailLoadedListener {

        @Override
        public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {

        }

        @Override
        public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {

        }

    }

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns
        // true
        // then it has handled the app icon touch event
        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

2 个答案:

答案 0 :(得分:1)

我使用以下代码:

        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener


        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.action_settings,  /* "open drawer" description */
                R.string.action_settings  /* "close drawer" description */
                )

看看它是否有效。 还要检查ic_drawer图像是否存在于不同大小的drawable-hdpi,drawable-mdpi,drawable-xhdpi和drawable-xxhdpi文件夹中。

答案 1 :(得分:1)

试试这段代码

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