15

我正在尝试在我的应用程序中添加导航抽屉。一切正常但现在我仍然得到箭头图标,尽管我用 Android 的 ic_drawer 替换了它?这是我的代码:

private ActionBarDrawerToggle mDrawerToggle;


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

    DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    ExpandableListView mDrawerList = (ExpandableListView) findViewById(R.id.left_drawer);

    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);
    mDrawerList.setAdapter(new DrawerListAdapter(this));
    mDrawerList.setOnChildClickListener(new DrawerItemClickListener());

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_launcher, R.string.drawer_open,
            R.string.drawer_close);

    Log.d("Mudit",
            "mDrawerToggle" + mDrawerToggle.isDrawerIndicatorEnabled()); // returns true

    mDrawerLayout.setDrawerListener(mDrawerToggle);

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

    getActionBar().setDisplayShowHomeEnabled(false);

}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

XML:

 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

        </LinearLayout>
    </ScrollView>

    <ExpandableListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#ffffff"
        android:dividerHeight="0dp" />

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

我在网上查看了各种代码示例,但他们都在做同样的事情。我不知道为什么它对我不起作用。

请帮忙。

4

4 回答 4

22

你必须使用

getActionBar().setDisplayShowHomeEnabled(true);

代替

getActionBar().setDisplayShowHomeEnabled(false);

干杯,

菲利克斯

编辑:这是通过 XML 隐藏图标的方法:

将此添加到您的样式。

<style name="ActionBarStyle" parent="android:Widget.Holo.Light.ActionBar.Solid">
    <item name="android:icon">@android:color/transparent</item>
</style>

然后将此添加到您在 AndroidManifest.xml 中定义的主题中(标准是 /res/values-v11/styles.xml 中的 AppBaseTheme - AppBaseTheme:

<item name="android:actionBarStyle">@style/ActionBarStyle</item>

希望这可以帮助!

于 2013-06-28T08:46:29.817 回答
22

试试这个代码

   @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        actbardrawertoggle.syncState();
    }
于 2014-02-06T13:14:30.273 回答
2

当我想从后退箭头取回菜单图标时,这对我有用

private void updateNavigationIcon() {
    if (getSupportFragmentManager().getBackStackEntryCount() == 0
            && getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        toggle.syncState();
    }
}
于 2016-06-01T03:19:59.287 回答
1

只需在 showGlobalContextActionBar() 方法(NavigationDrawerFragment.java 类)中添加以下行

actionBar.setHomeAsUpIndicator(R.drawable.ic_action_menu);

代码片段:

private void showGlobalContextActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_action_menu);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setTitle(R.string.app_name);
    }
于 2015-07-07T17:08:33.317 回答