2 回答
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 及以下版本——图标根本不会出现
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 没有为开发人员提供更简单的方法来更改整个应用程序的主题。
添加回答
举报
