有没有办法从颜色资源中获取color-int?我试图获取资源(R.color.myColor)中定义的颜色的单个红色,蓝色和绿色组件,以便我可以将三个搜索栏的值设置为特定级别.
有关可能有助于在搜索结果中显示此问题的另一个用例的更多信息,我想将alpha应用于我的资源中定义的颜色.
使用@ sat的正确答案:
__PRE__
sat*_*sat 871
您可以使用:
getResources().getColor(R.color.idname);
Run Code Online (Sandbox Code Playgroud)
在这里查看如何定义自定义颜色:
http://sree.cc/google/android/defining-custom-colors-using-xml-in-android
编辑(1):
由于getColor(int id)
现在已弃用,必须使用:
ContextCompat.getColor(context, R.color.your_color);
Run Code Online (Sandbox Code Playgroud)
(在支持库23中添加)
EDIT(2):
以下代码可用于棉花糖前后(API 23)
ResourcesCompat.getColor(getResources(), R.color.your_color, null); //without theme
ResourcesCompat.getColor(getResources(), R.color.your_color, your_theme); //with theme
Run Code Online (Sandbox Code Playgroud)
Ult*_*o_m 111
基于新的Android支持库(以及此更新),现在您应该致电:
ContextCompat.getColor(context, R.color.name.color);
Run Code Online (Sandbox Code Playgroud)
根据文件:
public int getColor (int id)
Run Code Online (Sandbox Code Playgroud)
此方法在API级别23中已弃用.请改用getColor(int,Theme)
它是同样的解决方案getResources().getColorStateList(id)
:
你必须改变它:
ContextCompat.getColorStateList(getContext(),id);
Run Code Online (Sandbox Code Playgroud)
Sur*_*gch 33
值/ color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- color int as #AARRGGBB (alpha, red, green, blue) -->
<color name="orange">#fff3632b</color>
...
<color name="my_view_color">@color/orange</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
int backgroundColor = ContextCompat.getColor(context, R.color.my_view_color);
// Color backgroundColor = ... (Don't do this. The color is just an int.)
myView.setBackgroundColor(backgroundColor);
Run Code Online (Sandbox Code Playgroud)
如果您当前的最小值。API级别是23,您可以getColor()
像我们使用的那样简单地使用getString()
:
//example
textView.setTextColor(getColor(R.color.green));
// if context is not available(ex: not in activity) use with context.getColor()
Run Code Online (Sandbox Code Playgroud)
如果你想要低于 API 级别23
,只需使用这个:
textView.setTextColor(getResources().getColor(R.color.green));
Run Code Online (Sandbox Code Playgroud)
但请注意,它getResources().getColor()
在 API Level 中已被弃用23
。在这种情况下,将上面替换为:
textView.setTextColor(ContextCompat.getColor(this /*context*/, R.color.green)) //Im in an activity, so I can use `this`
Run Code Online (Sandbox Code Playgroud)
ContextCompat:用于访问功能的帮助程序Context
如果您愿意,您可以使用SDK_INT
如下约束:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
textView.setTextColor(getColor(R.color.green));
} else {
textView.setTextColor(getResources().getColor(R.color.green));
}
Run Code Online (Sandbox Code Playgroud)
作为@sat的答案,获得颜色的好方法是
ResourcesCompat.getColor(getResources(), R.color.your_color, null);
Run Code Online (Sandbox Code Playgroud)
or use below way when you don't have access to getResources()
method.
Context context = getContext(); // like Dialog class
ResourcesCompat.getColor(context.getResources(), R.color.your_color, null);
Run Code Online (Sandbox Code Playgroud)
public void someMethod(){
...
ResourcesCompat.getColor(App.getRes(), R.color.your_color, null);
}
Run Code Online (Sandbox Code Playgroud)
It is most simple to use anywhere in your app! Even in Util class or any class where you don't have Context or getResource()
When you don't have Context
access, like a method in your Util
class.
Assume below method without Context.
public void someMethod(){
...
// can't use getResource() without Context.
}
Run Code Online (Sandbox Code Playgroud)
Now you will pass Context
as a parameter in this method and use getResources().
public void someMethod(Context context){
...
context.getResources...
}
Run Code Online (Sandbox Code Playgroud)
So here is a Bonus unique solution by which you can access resources from anywhere like Util class
.
Add Resources
to your Application
class or Create one if does not exist.
import android.app.Application;
import android.content.res.Resources;
public class App extends Application {
private static App mInstance;
private static Resources res;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
res = getResources();
}
public static App getInstance() {
return mInstance;
}
public static Resources getResourses() {
return res;
}
}
Run Code Online (Sandbox Code Playgroud)
Add name field to your manifest.xml
<application
tag. (If not added already)
<application
android:name=".App"
...
>
...
</application>
Run Code Online (Sandbox Code Playgroud)
Now you are good to go. Use ResourcesCompat.getColor(App.getRes(), R.color.your_color, null);
anywhere in app.
我已更新为使用,ContextCompat.getColor(context, R.color.your_color);
但有时(在某些设备/ Android版本上。我不确定)会导致NullPointerExcepiton。
因此,要使其在所有设备/版本上都能正常工作,在使用空指针的情况下,我会采用旧的方法。
try {
textView.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_grey_dark));
}
catch(NullPointerException e) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
textView.setTextColor(getContext().getColor(R.color.text_grey_dark));
}
else {
textView.setTextColor(getResources().getColor(R.color.text_grey_dark));
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
找到了一种更简单的方法:
Color.parseColor(getString(R.color.idname);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
318165 次 |
最近记录: |