【问题标题】:Setting the value for custom Attribute android设置自定义属性android的值
【发布时间】:2019-10-26 21:02:39
【问题描述】:

我有一个这样的自定义属性:

<attr name="colorPrimarySdk" format="color"/>
<attr name="colorSecondarySdk" format="color"/>
<attr name="colorAccentSdk" format="color"/>

我在我的风格中使用它们,如下所示:

<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
     <item name="colorPrimary">?colorPrimarySdk</item>
     <item name="colorPrimaryDark">?colorSecondarySdk</item>
     <item name="colorAccent">?colorAccentSdk</item>
</style>

现在我想要的是从代码中动态设置属性的值,比如:

colorPrimarySdk.value = myCustomColor

我已经尝试过使用 TypedValue 并访问属性本身。 如果有人可以帮助更改我的自定义属性的值,那将是一个很大的帮助。提前致谢。

【问题讨论】:

  • 这是通过样式设置它们。我想要一种可以访问 java 或 kotlin 文件中的属性并为该属性设置值的方法。
  • 您是否尝试修改现有主题或 any 属性?
  • @SarthakGandhi 你有没有找到以编程方式更改属性的任何解决方案?。

标签: android android-layout


【解决方案1】:

这很难:)

colors.xml:

    <?xml version="1.0" encoding="utf-8"?>
      <resources>
      <color name="your_special_color">#FF0099CC</color>
    </resources>

Res.java:

public class Res extends Resources {

    public Res(Resources original) {
        super(original.getAssets(), original.getDisplayMetrics(), original.getConfiguration());
    }

    @Override public int getColor(int id) throws NotFoundException {
        return getColor(id, null);
    }

    @Override public int getColor(int id, Theme theme) throws NotFoundException {
        switch (getResourceEntryName(id)) {
            case "your_special_color":
                // You can change the return value to an instance field that loads from SharedPreferences.
                return Color.RED; // used as an example. Change as needed.
            default:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    return super.getColor(id, theme);
                }
                return super.getColor(id);
        }
    }
}

BaseActivity.java

public class BaseActivity extends AppCompatActivity {

    ...

    private Res res;

    @Override public Resources getResources() {
        if (res == null) {
            res = new Res(super.getResources());
        }
        return res;
    }

    ...

}

【讨论】:

    【解决方案2】:

    在颜色文件中为特定主题定义颜色:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="my_link_color1">#0077CC</color>
        <color name="my_link_color2">#626262</color>
    </resources>
    

    使用内容创建文件 res/values/attrs.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <attr name="myLinkColor" format="reference" />
    </resources>
    

    假设我们的 styles.xml 中有 2 个主题(Theme1 和 Theme2)定义:

    <style name="Theme1" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="myLinkColor">@color/my_link_color1</item>
    </style>
    
    <style name="Theme2" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="myLinkColor">@color/my_link_color2</item>
    </style>
    

    使用 XML 中的颜色:

    android:textColor="?attr/myLinkColor"
    

    【讨论】:

    • 您是否真的阅读了 OP 发布的内容?我将它粘贴在这里:我想要的是从代码中动态设置属性的值。你的例子与这样做无关。 ://
    • 我的答案有变化,你能查一下吗?并参考链接mobikul.com/…
    • 您在此处显示的内容很好,但这不是 OP 所要求的。 OP 希望能够获取 myLinkColor 并为其分配不同的值(据我了解)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多