1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android 获取手机颜色代码 Android获取当前主题的颜色属性

android 获取手机颜色代码 Android获取当前主题的颜色属性

时间:2023-05-17 03:09:18

相关推荐

android 获取手机颜色代码 Android获取当前主题的颜色属性

安卓代码中获取当前主题颜色方法的两种方法:

第一种方法:

TypedArray array = getTheme().obtainStyledAttributes(new int[]{

android.R.attr.colorBackground,

android.R.attr.textColorPrimary,

android.R.attr.colorPrimary,

android.R.attr.colorPrimaryDark,

android.R.attr.colorAccent,

});

backgroundColor = array.getColor(0, 0xFF00FF);

textColor = array.getColor(1, 0xFF00FF);

colorPrimary = array.getColor(2, getResources().getColor(R.color.colorPrimary));

colorPrimaryDark = array.getColor(3, getResources().getColor(R.color.colorPrimaryDark));

colorAccent = array.getColor(4, getResources().getColor(R.color.colorAccent));

array.recycle();

第二种方法:

public int getColorPrimary(){

TypedValue typedValue = new TypedValue();

getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);

return typedValue.data;

}

public int getDarkColorPrimary(){

TypedValue typedValue = new TypedValue();

getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);

return typedValue.data;

}

public int getColorAccent(){

TypedValue typedValue = new TypedValue();

getTheme().resolveAttribute(R.attr.colorAccent, typedValue, true);

return typedValue.data;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。