安卓代码中获取当前主题颜色方法的两种方法:
第一种方法:
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;
}