반응형
프로그래밍 방식으로 강조 색상을 얻는 방법은 무엇입니까?
프로그래밍 방식으로 아래와 같이 스타일에 설정된 강조 색상을 어떻게 가져올 수 있습니까?
<item name="android:colorAccent">@color/material_green_500</item>
다음과 같은 방법으로 현재 테마에서 가져올 수 있습니다.
private int fetchAccentColor() {
TypedValue typedValue = new TypedValue();
TypedArray a = mContext.obtainStyledAttributes(typedValue.data, new int[] { R.attr.colorAccent });
int color = a.getColor(0, 0);
a.recycle();
return color;
}
이것은 나에게도 효과적이었습니다.
public static int getThemeAccentColor (final Context context) {
final TypedValue value = new TypedValue ();
context.getTheme ().resolveAttribute (R.attr.colorAccent, value, true);
return value.data;
}
private static int getThemeAccentColor(Context context) {
int colorAttr;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
colorAttr = android.R.attr.colorAccent;
} else {
//Get colorAccent defined for AppCompat
colorAttr = context.getResources().getIdentifier("colorAccent", "attr", context.getPackageName());
}
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(colorAttr, outValue, true);
return outValue.data;
}
현재 테마에서 색상을 가져 오는 utils 클래스에 정적 메서드가 있습니다. 대부분의 경우 colorPrimary, colorPrimaryDark 및 accentColor이지만 더 많은 정보를 얻을 수 있습니다.
@ColorInt
public static int getThemeColor
(
@NonNull final Context context,
@AttrRes final int attributeColor
)
{
final TypedValue value = new TypedValue();
context.getTheme ().resolveAttribute (attributeColor, value, true);
return value.data;
}
Kotlin을 사용하는 분들을 위해
fun Context.themeColor(@AttrRes attrRes: Int): Int {
val typedValue = TypedValue()
theme.resolveAttribute (attrRes, typedValue, true)
return typedValue.data
}
이것에 대한 나의 견해는 다음과 같습니다.
public static String getThemeColorInHex(@NonNull Context context, @NonNull String colorName, @AttrRes int attribute) {
TypedValue outValue = new TypedValue();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
context.getTheme().resolveAttribute(attribute, outValue, true);
} else {
// get color defined for AppCompat
int appCompatAttribute = context.getResources().getIdentifier(colorName, "attr", context.getPackageName());
context.getTheme().resolveAttribute(appCompatAttribute, outValue, true);
}
return String.format("#%06X", (0xFFFFFF & outValue.data));
}
용법:
String windowBackgroundHex = getThemeColorInHex(this, "windowBackground", android.R.attr.windowBackground);
String primaryColorHex = getThemeColorInHex(this, "colorPrimary", R.attr.colorPrimary);
Kotlin 솔루션 :
context.obtainStyledAttributes(TypedValue().data, intArrayOf(R.attr.colorAccent)).let {
Log.d("AppLog", "color:${it.getColor(0, 0).toHexString()}")
it.recycle()
}
참고 URL : https://stackoverflow.com/questions/27611173/how-to-get-accent-color-programmatically
반응형
'program tip' 카테고리의 다른 글
명령 줄을 통해 OS X에서 간단한 Hello World 프로그램 컴파일 (0) | 2020.10.09 |
---|---|
바이트 배열을 Stream으로 변환하는 방법 (0) | 2020.10.09 |
ES6 클래스에서 "공개 정적 필드"를 만들려면 어떻게합니까? (0) | 2020.10.09 |
Composer-요청한 PHP 확장 mbstring이 시스템에 없습니다. (0) | 2020.10.09 |
클릭시 확인란이 선택되어 있지 않은지 확인하십시오-jQuery (0) | 2020.10.09 |