相关疑难解决方法(0)

如何更改工具栏导航和溢出菜单图标(appcompat v7)?

我在使用v7工具栏时遇到了困难.曾经简单的任务ActionBar,现在似乎过于复杂.无论我设置什么样的风格,我都无法更改导航图标(打开抽屉)或溢出菜单图标(打开菜单).

所以我有一个 Toolbar

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ghex"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.Light"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    >
Run Code Online (Sandbox Code Playgroud)

我编码看起来像这样

//before in the code I do
mToolbar = (Toolbar) findViewById(R.id.toolbar);

private void initToolbar() {
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
}
Run Code Online (Sandbox Code Playgroud)

现在,我需要更改Drawable这两个图标.

我如何为compat v7做到这一点Toolbar?我想我需要在抽屉打开时更改箭头(Android 5.0).

android android-appcompat android-layout material-design android-toolbar

32
推荐指数
3
解决办法
9万
查看次数

通过主题或样式更改工具栏导航图标的色调或颜色

我可以通过代码在工具栏中设置带有色调的导航图标:

  Toolbar toolbar = findViewById(R.id.biometry_agreement_toolbar);
  Drawable drawable = AppCompatResources.getDrawable(requireContext(), R.drawable.ic_24_close);
  drawable.setColorFilter(ColorGenerator.buildColorFilterByAttr(toolbar.getContext(), R.attr.colorMaskColored));
  toolbar.setNavigationIcon(drawable);
Run Code Online (Sandbox Code Playgroud)

或者我可以在 xml 中设置这样的图标,但在这种情况下我不能设置色调,因为没有属性 navigationIconTint:

<androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:navigationIcon="@drawable/ic_24_close"
            app:titleTextColor="@color/color_primary"
            tools:title="@string/title"/>
Run Code Online (Sandbox Code Playgroud)

是否可以在没有 Java 代码的情况下以某种方式为导航图标设置色调?或将自定义图标设置到工具栏中会清除所有色调和颜色?

我不喜欢从 java 设置图标,因为工具栏初始化没有很好的代码重用。

我尝试了不同的自定义样式,覆盖了 colorControlNormal,但没有运气。

提前致谢

android styling android-theme android-toolbar

6
推荐指数
1
解决办法
4869
查看次数

android - 更改 MaterialToolbar 菜单项图标颜色

我目前正在创建一个应用程序,它有一个MaterialToolbar小部件。我想将图标颜色设置为白色。我尝试按照这个问题中接受的答案进行操作,但是,它不起作用。添加colorControlNormalstyles.xml 不起作用。

这是我的 MaterialToolbar xml 代码:

<com.google.android.material.appbar.MaterialToolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/topToolbar"
            android:background="@color/colorPrimaryDark"
            app:title="Revo"
            app:titleTextColor="@android:color/white"
            app:menu="@menu/menu_floatingsearchview"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
Run Code Online (Sandbox Code Playgroud)

我能做什么?

编辑、解决方案和说明

谢谢大家的好答案。我设法找到了一个解决方案,其中包括两个解决方案和另一个问题。

这个问题中,被问到为什么colorControlNormal不起作用。接受的答案说,在向量行中,您必须更改给 的值android:fillColor,并将其替换为?attr/colorControlNormal。执行此技巧,项目 colorControlNormal,将控制所需的图标颜色。

在应用样式中,您需要放置:

<item name="colorControlNormal">@android:color/white</item>

然后,在所需的图标中,您需要放置在path

android:fillColor="?attr/colorControlNormal"

就是这样!现在图标将获得赋予 colorControlNormal 属性的颜色!

android android-xml android-theme android-toolbar material-components-android

5
推荐指数
1
解决办法
2971
查看次数