【问题标题】:Change Icon Of Navigation Drawer更改导航抽屉的图标
【发布时间】:2019-10-03 01:51:46
【问题描述】:

我无法将导航抽屉图标更改为自定义图标。我目前必须实现顶部有 3 条水平线的标准抽屉图标,但现在我想用我的自定义抽屉图标替换它。

这就是我的mDrawerToggle此刻的样子:

mDrawerToggle=new ActionBarDrawerToggle(this,
    mDrawerLayout,
    R.drawable.app_icon,
    R.string.drawer_open) {
        // My code
    };

【问题讨论】:

  • 以上是你目前的情况吗?
  • 对@FarbodSalamat-Zadeh
  • 尝试用图标设置 homeAsUpIndicator()。
  • @Harry 我也试过了...mDrawerToggle.setHomeAsUpIndicator(R.drawable.app_icon);

标签: android android-actionbar android-drawable android-navigation android-actionbaractivity


【解决方案1】:

使用下面的代码,它适用于 V7 ActionBarDrawerToggle

mDrawerToggle.setDrawerIndicatorEnabled(false);

mDrawerToggle.setHomeAsUpIndicator(R.drawable.your_custom_icon);
 mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
 public void onClick(View v) {
     if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
          mDrawerLayout.closeDrawer(GravityCompat.START);
     } else {
         mDrawerLayout.openDrawer(GravityCompat.START);
    }
}
});

【讨论】:

  • 建议:您可以使用 Drawable 资源 id 将图像文件设置为 Navigation Drawer 图标。示例:mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_navigation_home_icon);因此,无需使用以下代码:Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.your_custom_icon, getActivity().getTheme());
  • 这对我来说是最好的选择。显式设置图标,而不是让 Android 决定是显示菜单图标还是返回图标,对我的应用来说更加一致。
  • 为什么图标不自动调整大小。当我使用 512*512 的图标时,它会占据整个 ActionBar。
【解决方案2】:

以下是取自的示例代码 Creating a Navigation Drawer

Activity.class

public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    ...

    public void onCreate(Bundle savedInstanceState) {
        ...

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

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

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

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);

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

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

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (mDrawerToggle.onOptionsItemSelected(item)) {
          return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }

    ...
}

【讨论】:

  • Yar @Tarun jain 我也做过同样的事情...我的问题是什么检查...我知道 mdraweroogle 的构造函数
  • @Tufan 你添加了以下代码吗..@Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); }
  • 我已经覆盖了这两种方法......看看我的照片我的图标显示但是当我想改变..它没有改变
  • 如果我添加 R.String.drawer_close 那么它会给我错误更改构造或将 ic_drawer 更改为工具栏..任何解决方案
  • 现在不工作
【解决方案3】:

这是主要活动

final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);


        toggle.setDrawerIndicatorEnabled(false);

        toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawer.openDrawer(GravityCompat.START);
            }
        });

        toggle.setHomeAsUpIndicator(R.drawable.menuicon);

【讨论】:

    【解决方案4】:

    您可以将此格式用于您的mDrawerToggle

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
        R.drawable.CUSTOM_ICON, // Navigation menu toggle icon
        R.string.DRAWER_OPEN, // Navigation drawer open description
        R.string.DRAWER_CLOSE // Navigation drawer close description
        )
    

    更改您的可绘制对象并确保它与代码中的名称相同。

    【讨论】:

    • 此 CTOR 不存在。只有类似的事情是拥有 DrawerArrowDrawable 而不是可绘制资源
    【解决方案5】:

    这是主布局文件

    <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" >
    
        <!-- Framelayout to display Fragments -->
    
        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <!-- Listview to display slider menu -->
    
        <ListView
            android:id="@+id/list_slidermenu"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/white"
            android:choiceMode="singleChoice"
            android:divider="@color/black"
            android:dividerHeight="1dp" />
    
    </android.support.v4.widget.DrawerLayout>
    

    这是主要活动

    DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                    R.drawable.menuicon, // nav menu toggle icon
                    R.string.app_name, // nav drawer open - description for
                                        // accessibility
                    R.string.app_name // nav drawer close - description for
                                        // accessibility
            ) {
                public void onDrawerClosed(View view) {
                    // getActionBar().setTitle(mTitle);
                    // calling onPrepareOptionsMenu() to show action bar icons
                    invalidateOptionsMenu();
                }
    
                public void onDrawerOpened(View drawerView) {
                    // getActionBar().setTitle(mDrawerTitle);
                    // calling onPrepareOptionsMenu() to hide action bar icons
                    invalidateOptionsMenu();
                }
            };
            mDrawerLayout.setDrawerListener(mDrawerToggle);
    

    终于在 R.drawable.menuicon(你可以给你的图像 id)它会工作。

    【讨论】:

    • 每当我想用你的代码更改我的图标时,它都会给我将应用程序图标更改为工具栏的错误...你能告诉我你使用了哪个文件扩展名
    • 对于我使用 png 图像但任何扩展图像都可以使用的图像
    • 如果我按照你的代码@Allu 它给我错误构造函数是未定义的
    • 查看此链接我已关注此链接它对我有用,如果可能,请在此链接中检查总代码一次androidhive.info/2013/11/…
    • 如果我遵循你的代码,它会给我错误 no constructor match for mdraweroogle
    猜你喜欢
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多