Android的图标在工具栏

Android的图标在工具栏

本文介绍了更改ActionBarDrawerToggle Android的图标在工具栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有抽屉式导航栏和工具栏的活动

I have an activity with navigation drawer and toolbar

活动

public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private CustomTextViewMondaRegular tvTitle;
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);

    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    tvTitle = (CustomTextViewMondaRegular) findViewById(R.id.actionTitle);
    tvTitle.setText(getIntent().getStringExtra("title"));
    mDrawerList.setAdapter(new NavAdapter(getApplicationContext()));
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,toolbar,
            R.string.title_activity_main, R.string.title_activity_main) {
        @Override
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu();
            // creates call to onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    if (savedInstanceState == null) {

    }
}

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

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

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        Logger.e("pos", position + "");
    }
}

}

活动XML

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

<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <android.support.v7.widget.Toolbar
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/white"
        android:elevation="3dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/MyDarkToolbarStyle">

        <TextView
            android:id="@+id/actionTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:drawableEnd="@drawable/logo"
            android:drawableRight="@drawable/logo"
            android:gravity="center"
            android:text="XXXXXX"
            android:textColor="#000000"
            android:textSize="18sp" />

    </android.support.v7.widget.Toolbar>
</FrameLayout>

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    android:choiceMode="singleChoice"
    android:divider="#D8D8D8" />

的切换图标目前显示的默认图标。我需要改变图标。我该怎么做?

The toggle icon currently shows the default icon. I need to change that icon. How can I do that?

推荐答案

有关谁在将来遇到这太问题的人,

For people who come across this SO question in future,

我们必须禁用抽屉指标

mDrawerToggle.setDrawerIndicatorEnabled(false);

,然后将工具栏的导航按钮

 mToolbar.setNavigationIcon(R.drawable.navIcon);

P.S。之后,我们必须设置导航点击工具栏听者和开放 NavigationDrawer manualy上。
喜欢
mToolbar.setNavigationOnClickListener :D

P.S. after that we have to set Navigation click listner on toolbar and open NavigationDrawer manualy.likemToolbar.setNavigationOnClickListener :D

这篇关于更改ActionBarDrawerToggle Android的图标在工具栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:15