답변:
이것이 당신의 대답입니다
colorStr=getResources().getString(R.color.someColor);
당신은 얻을 것이다
colorStr = "#123456"
//noinspection ResourceType진술 바로 위에 추가 할 수 있습니다 .
getResources().getString(0+R.color.someColor);작품
쉬운 카피 파스타를 위해 :
"#" + Integer.toHexString(ContextCompat.getColor(getActivity(), R.color.some_color));
또는 투명도없이 원하는 경우 :
"#" + Integer.toHexString(ContextCompat.getColor(getActivity(), R.color.some_color) & 0x00ffffff);
'#' + Integer.toHexString(getResources().getColor(R.color.someColor);후에 내가 그것을 사용 이후Color.parseColor
원인 getResources().getColor은 api> 23이 필요합니다. 따라서 이것이 더 좋습니다. 쉽게 복사 및 붙여 넣기를 위해 :
Integer.toHexString( ContextCompat.getColor( getContext(), R.color.someColor ) );
또는 투명도없이 원하는 경우 :`
Integer.toHexString( ContextCompat.getColor( getContext(), R.color.someColor ) & 0x00ffffff );
ContextCompat.getColor3 개 인수를 고려하지 않습니다
오류가 발생하면 @SuppressLint ( "ResourceType")을 추가합니다. 우는 소리처럼.
private String formatUsernameAction(UserInfo userInfo, String action) {
String username = userInfo.getUsername();
@SuppressLint("ResourceType") String usernameColor = getContext().getResources().getString(R.color.background_button);
return "<font color=\""+usernameColor+"\">" + username
+ "</font> <font color=\"#787f83\">" + action.toLowerCase() + "</font>";
}
당신이 사용하지 않는 경우 ContextCompat또는 SuppressLint, 단순히 추가 string하여 아래 자원 권리를 color.
대신에
<color name="text_color">#FFFFFF</color>
사용하다
<color name="text_color">#FFFFFF</color>
<string name="text_color_string">#FFFFFF</string>
코드는 더 반복되지만 훨씬 더 깔끔합니다. 게다가 string변경 후 업데이트하는 것을 잊는 color것은 바로 옆에 있기 때문에 거의 불가능합니다.