问题:有没有办法在 Android XML 文件中的 XML“位图”上动态设置“src”属性?
我有一个名为“repeat_background.xml”的自定义 XML 可绘制对象,定义如下:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/background"
android:tileMode="repeat" />
我使用这个drawable来“平铺”一个1x1 jpg(名为background.jpg)作为我应用程序中所有页面的背景,它工作得很好 - 这是一个将它设置为背景的示例LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/repeat_background">
但是,我希望实际的 jpg(在本例中为来自 @drawable/background 的“background.jpg”)基于用户的偏好 - 所以我将有一个允许用户选择红色、蓝色、橙色等的列表. 所以用户可以覆盖应用程序中显示的背景颜色,我将有一个 1x1 jpg 对应于我的资源包中的每种可用颜色 - 但是我如何将首选 jpg 显示为背景?
我不想在每个 Fragment 或 Activity 中手动调用一些代码,我想要一些尊重用户偏好并做出相应反应的东西。
我试图扩展 BitmapDrawable 类,但没有走得太远。非常感谢任何有关我如何完成此任务的建议。