在Android抽屉上两个图标出现在ActionBar上,只需要一个图标

时间:2015-10-10 08:53:17

标签: android

我在我的Android应用程序中实现导航抽屉。但问题是动作栏上出现两个图标。我添加了ActionBarDrawerToggle,似乎从ActionBarDrawerToggle显示第二个图标。请告诉我我的代码中的错误在哪里。

enter image description here

请告诉我小麦可能是错误的。

这是完整的代码

package com.example.newdrawer;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

    DrawerLayout mDrawerLayout;
    ActionBarDrawerToggle mActionBarDrawerToggle;

    ListView listView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getActionBar().setIcon(R.drawable.ic_launcher);

        String[] listItems = {"first", "second", "third"};

        listView = (ListView) findViewById(R.id.drawer);

        listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.textView1, listItems));

        listView.setOnItemClickListener(new DrawerItemClickListener());

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

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mActionBarDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_launcher,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_opened,  /* "open drawer" description */
                R.string.drawer_closed  /* "close drawer" description */
                ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getActionBar().setTitle("appname");
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActionBar().setTitle("select item");
            }
        };

        mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    // to change up caret
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mActionBarDrawerToggle.syncState();
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig){
        super.onConfigurationChanged(newConfig);
        mActionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.
       if (mActionBarDrawerToggle.onOptionsItemSelected(item)) {
           return true;
       }

       return super.onOptionsItemSelected(item);
    }

    // navigation drawer click listener
    private class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, "You clicked:"+String.valueOf(position), Toast.LENGTH_LONG).show();
            mDrawerLayout.closeDrawers();
        }

    }

}

这是我的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView 
            android:layout_width = "match_parent"
            android:layout_height= "match_parent"
            android:id = "@+id/text1"
            android:text = "Hello World"/>

        <ListView 
            android:id="@+id/drawer"
            android:layout_width = "210dp"
            android:layout_height = "match_parent"
            android:layout_gravity = "start"
            android:background="@android:color/white"/>

    </android.support.v4.widget.DrawerLayout>"

</LinearLayout>

提前谢谢

1 个答案:

答案 0 :(得分:2)

第一个图标应该是抽屉图标(又名汉堡包)。但你用启动器图标替换它

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

R.drawable.ic_launcher替换为上面代码中的实际抽屉图标资源。