how to get Theme attributes values

后端 未结 3 481
梦谈多话
梦谈多话 2020-12-29 02:54

Is it possible to obtain styled attributes values from particular Theme without setting the theme up to application/activity? (I mean before invoking context.setTheme(

3条回答
  •  长发绾君心
    2020-12-29 03:30

    For example, to get editTextColor attribute's value of a theme called MyTheme:

    TypedArray a = getTheme().obtainStyledAttributes(
            R.style.MyTheme,
            new int[] { R.attr.editTextColor });
    
    // Get color hex code (eg, #fff)
    int intColor = a.getColor(0 /* index */, 0 /* defaultVal */);
    String hexColor = Integer.toHexString(intColor);
    
    // Don't forget to recycle
    a.recycle();
    

提交回复
热议问题