【问题标题】:Cannot change navigation drawer icon color android无法更改导航抽屉图标颜色android
【发布时间】:2021-01-30 05:28:21
【问题描述】:

我尝试了以下代码,但无法获得导航图标的颜色。 这是下面的代码。

主要活动:

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setNavigationIcon(R.drawable.ic_menu);
    drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_orders, R.id.nav_withdraw, R.id.nav_myteam, R.id.nav_refer,
            R.id.nav_notice, R.id.nav_message, R.id.nav_add_account, R.id.nav_account_detail, R.id.nav_recharge_record, R.id.nav_withdraw_record,
            R.id.nav_complaints, R.id.nav_aboutus, R.id.nav_signout)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);


    @Override
    public boolean onSupportNavigateUp() {
         NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
         return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
    

android新的android studio版本提供了内置的导航抽屉,也很简单。但我无法更改导航图标的颜色。

主要活动布局:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
        
        <include
            layout="@layout/navigation_app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
            
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            style="@style/Theme.MenuText"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/navigation_header_main"
            app:menu="@menu/activity_main_drawer" />

    </androidx.drawerlayout.widget.DrawerLayout>

这是主要活动的主要布局。

以及片段调用的包含布局。

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        style="@style/Theme.MenuIcon"
        android:layout_height="match_parent"
        tools:context=".ui.P001aaa.P001bbb.P001ccc.P001ddd.P001eee.P001fff.P001ggg.P001hhh.Activity.Main_Activity">

        <com.google.android.material.appbar.AppBarLayout
            style="@style/Theme.Datas"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                style="@style/Theme.Datas"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize" />

        </com.google.android.material.appbar.AppBarLayout>

        <include layout="@layout/navigation_content_main" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

从下图你可以明白我想要什么样的改变。

我想改变这个图标颜色:

This is the image.

【问题讨论】:

  • 你为什么使用style?这仅适用于该视图本身,不适用于它的子视图(这就是 styleandroid:theme 之间的区别)。你的Theme.Datas 是什么?

标签: java android colors icons navigation-drawer


【解决方案1】:

在这个实现了主题的新android studio的例子中,我们只需要在theme.xml中声明样式

<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="color">@color/white</item>
</style>

【讨论】:

    【解决方案2】:

    您必须先禁用默认的抽屉指示器,然后才能使用自定义图标,例如 Drawable 或带有 setHomeAsUpIndicator 方法的 DrawableRes。

    toggle.setDrawerIndicatorEnabled(false)
    toggle.setHomeAsUpIndicator(R.drawable.custom_nav_icon);
    

    【讨论】:

      【解决方案3】:

      只需在您的 onCreate 函数中使用它:

      toggle.setHomeAsUpIndicator(R.drawable.nav_icon);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-23
        • 1970-01-01
        • 2016-12-22
        • 1970-01-01
        • 1970-01-01
        • 2016-12-20
        • 2015-10-16
        • 1970-01-01
        相关资源
        最近更新 更多