我有(不幸的是。不管怎样......)颜色格式,用于html:
...
tabLayout.setBackgroundColor(getTabLayoutBackgroundColor(jsonObject));
...
private int getTabLayoutBackgroundColor(JSONObject jsonObject) {
// tab color
String[] rgba_tab = new String[4];
try {
rgba_tab = jsonObject.getString("navbar-background-color").split("\\(")[1].split("\\)")[0].split(",");
} catch (JSONException e) {
e.printStackTrace();
}
return Color.argb(Integer.parseInt(rgba_tab[3]), Integer.parseInt(rgba_tab[0]), Integer.parseInt(rgba_tab[1]), Integer.parseInt(rgba_tab[2]));
}
我想将其转换为Android原生十六进制颜色格式#AARRGGBB。 任何人都可以帮助我。
我尝试了以下方式,但它没有奏效:
{
"primary-color": "rgba(214,34,48,1)",
"background-color": "default",
"navbar-background-color": "rgba(214,34,48,1)",
"navbar-font-color": "rgba(255,255,255,1)",
"font": "Arial"
}
我必须解析以下响应,动态设置颜色到我的控件,如ToolBar,TabLayout背景和TabLayout文本颜色:
this.splice(this.lenght, 0, someArgument)
答案 0 :(得分:2)
尝试使用:
String hex = String.format("#%02x%02x%02x%02x", alpha,red, green, blue);
答案 1 :(得分:0)
试试这个: 颜色c =新颜色(i,j,k); Integer.toHexString(c.getRGB()& 0x00ffffff));
答案 2 :(得分:0)
这是我将 rgba 的颜色转换为android颜色格式的功能:
@ColorInt
int parseRgba(@NonNull String colorString, @ColorInt int defaultColor) {
String cs = colorString.replaceAll("\\s", "");
if (cs.startsWith("rgba(") && cs.endsWith(")")) {
String[] components = cs
.substring(5, cs.length() - 1)
.split(",");
if (components.length == 4) {
try {
int red = Integer.parseInt(components[0]);
int green = Integer.parseInt(components[1]);
int blue = Integer.parseInt(components[2]);
float alphaF = Float.parseFloat(components[3]);
if (0 <= red && red <= 255 &&
0 <= green && green <= 255 &&
0 <= blue && blue <= 255 &&
0f <= alphaF && alphaF <= 1f) {
return Color.argb((int) (alphaF * 255), red, green, blue);
}
} catch (NumberFormatException ignored) {
}
}
}
return defaultColor;
}