9

我是 Android 新手,在我的应用程序中,我正在使用DrawerLayout,没关系。但我想改变ActionBarDrawerToggle图标。

我们怎样才能改变它?

我尝试了很多,但我无法达到预期的结果,请帮助我吗?

xml:-

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:local="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </LinearLayout>

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

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

活动:-

public class MainActivity extends AppCompatActivity {
    ActionBarDrawerToggle actionBarDrawerToggle;
    DrawerLayout drawerLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
        drawerLayout.setDrawerListener(actionBarDrawerToggle);
    }
4

4 回答 4

14

将此行添加到您的工具栏

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
ActionBarDrawerToggle mToogle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
    drawerLayout.addDrawerListener(mToogle);
    mToogle.syncState();
toolbar.setNavigationIcon(R.drawable.ic_your_image);

mToogle.syncState() 将负责操作(打开和关闭导航抽屉)。

于 2016-03-21T15:19:34.133 回答
1

你必须 3 个步骤:

  1. 添加actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
  2. 添加toolbar.setNavigationIcon(R.drawable.menu_icon);
  3. 将 OnClick 添加到新图标,如下所示:

    toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {drawerLayout.openDrawer(Gravity.LEFT); } });

于 2016-03-21T15:38:01.227 回答
0

在为此苦苦挣扎了几个小时之后,这就是我终于让它工作的方式:

我的实现是使用Toolbarand android.support.v7.app.ActionBarDrawerToggle

使用这些行将onCreate()设置Toolbar为支持ActionBar

@Override
public void onCreate(Bundle savedInstanceState)
{
      toolbar = (Toolbar) findViewById(getToolbarId());

      setSupportActionBar(toolbar);
      ActionBar ab = getSupportActionBar();
      if (ab != null)
      {
            ab.setDisplayHomeAsUpEnabled(true);
            ab.setHomeButtonEnabled(true);
      }
}

然后您需要更改 NavDrawer 图标,您将使用此代码来完成getSupportActionBar().setHomeAsUpIndicator(R.drawable.my_icon);。但是你不能从任何地方调用它,你需要在同步 DrawerToggle 状态之后调用它。

所以这样称呼它:

    drawerToggle.syncState();
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.my_icon);
于 2017-02-23T18:29:41.807 回答
-1

它可能是正确的:-P

public class MainActivity extends AppCompatActivity {
    ActionBarDrawerToggle actionBarDrawerToggle;
    DrawerLayout drawerLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer);
        drawerLayout = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                drawerLayout ,         /* DrawerLayout object */
                R.drawable.drawer_icon,  /* nav drawer icon to replace 'Up' caret */
                "drawer_open",  /* "open drawer" description */
                "drawer_close"  /* "close drawer" description */
                )
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
        drawerLayout.setDrawerListener(actionBarDrawerToggle);
    }
于 2016-03-21T15:17:05.797 回答