猿问

有没有办法更改首选项标题中图标的颜色?

我已经在我的首选项头文件中添加了一些图标(作为 SVG),我想用 Java 更改它们的颜色(我的应用程序是主题化的,我找不到任何其他方法来根据主题更改图标颜色) .

我已经尝试过以类似于按钮等的方式更改图标的颜色......我也无法使用“app:tint”属性更改颜色并且无论如何它都不会随着主题而改变我愿意。

这是首选项标头代码。我想改变“ic_round_settings”的颜色。

<header
     android:fragment="com.appname.settings.fragment.GeneralSettingsFragment"
        android:icon="@drawable/ic_round_settings"
        android:title="@string/settings_general"
        android:summary="@string/settings_general_explain" />


红糖糍粑
浏览 169回答 2
2回答

Qyouu

好的,我找到了一种方法。如果您将 attr 属性添加到首选项标头,如下所示:<header &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:fragment="com.appname.settings.fragment.GeneralSettingsFragment" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:icon="?attr/ic_round_settings" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:title="@string/settings_general" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:summary="@string/settings_general_explain"&nbsp;/>并将该属性添加到值文件夹中的 attr.xml 文件:<attr&nbsp;name="ic_round_settings"&nbsp;format="reference"/>并将其添加到带有明暗版本图标的 styles.xml 中的主题类中,主题将发生变化:<style&nbsp;name="Theme.BaseLightTheme"&nbsp;parent="Theme.AppCompat"> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="ic_round_settings">@drawable/ic_round_settings_dark</item> </style> <style&nbsp;name="Theme.BaseDarkTheme"&nbsp;parent="Theme.AppCompat"> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="ic_round_settings">@drawable/ic_round_settings_light</item> </style>在 SVG 图标文件中,将浅色 SVG 图标副本中的颜色从#000000 更改为#ffffff:<path &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:fillColor="#000000" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:pathData=""/>编辑:这不适用于 Android 4.4 及以下版本——图标根本不会出现

森林海

要实现该行为,请像这样使用对 XML 中颜色的引用<FrameLayout&nbsp;&nbsp; &nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent">&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/btn_send"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textColor="?attr/colorAccent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:tint="?attr/colorAccent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/chat_send_text"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:drawableTint="?attr/colorAccent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:drawableRight="@drawable/ic_paper_plane"/></FrameLayout>此外,在处理活动时**确保在使用**之前设置主题,否则在动态设置主题时setContentView(R.layout_your_layout_file)必须调用。recreate()例子override fun onCreate(savedInstanceState: Bundle?) {&nbsp; &nbsp; setTheme(whatever_theme_you_want_to_use)&nbsp; &nbsp; setContentView(R.layout.activity_cool)&nbsp; &nbsp; // Further view initialization}缺点是您必须setTheme在所有活动中明确指出,因为 Android 没有为开发人员提供更简单的方法来更改整个应用程序的主题。
随时随地看视频慕课网APP

相关分类

Java
我要回答