为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

红糖糍粑 2023-03-23 13:41:38
我已经在我的首选项头文件中添加了一些图标(作为 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" />
查看完整描述

2 回答

?
Qyouu

TA贡献1786条经验 获得超11个赞

好的,我找到了一种方法。如果您将 attr 属性添加到首选项标头,如下所示:

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

并将该属性添加到值文件夹中的 attr.xml 文件:

<attr name="ic_round_settings" format="reference"/>

并将其添加到带有明暗版本图标的 styles.xml 中的主题类中,主题将发生变化:

<style name="Theme.BaseLightTheme" parent="Theme.AppCompat">
    <item name="ic_round_settings">@drawable/ic_round_settings_dark</item>
</style>
<style name="Theme.BaseDarkTheme" parent="Theme.AppCompat">
    <item name="ic_round_settings">@drawable/ic_round_settings_light</item>
</style>

在 SVG 图标文件中,将浅色 SVG 图标副本中的颜色从#000000 更改为#ffffff:

<path
      android:fillColor="#000000"
      android:pathData=""/>

编辑:这不适用于 Android 4.4 及以下版本——图标根本不会出现


查看完整回答
反对 回复 2023-03-23
?
森林海

TA贡献2011条经验 获得超2个赞

要实现该行为,请像这样使用对 XML 中颜色的引用


<FrameLayout 

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"

    android:layout_height="match_parent">


    <TextView

                android:id="@+id/btn_send"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:textColor="?attr/colorAccent"

                android:tint="?attr/colorAccent"

                android:text="@string/chat_send_text"

                android:drawableTint="?attr/colorAccent"

                android:drawableRight="@drawable/ic_paper_plane"/>

</FrameLayout>

此外,在处理活动时**确保在使用**之前设置主题,否则在动态设置主题时setContentView(R.layout_your_layout_file)必须调用。recreate()


例子

override fun onCreate(savedInstanceState: Bundle?) {

    setTheme(whatever_theme_you_want_to_use)

    setContentView(R.layout.activity_cool)

    // Further view initialization

}

缺点是您必须setTheme在所有活动中明确指出,因为 Android 没有为开发人员提供更简单的方法来更改整个应用程序的主题。


查看完整回答
反对 回复 2023-03-23
  • 2 回答
  • 0 关注
  • 168 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号