首页 文章

从颜色资源中获取color-int

提问于
浏览
355

有没有办法从颜色资源中获取color-int?我试图获取资源(R.color.myColor)中定义的颜色的单个红色,蓝色和绿色组件,以便我可以将三个搜索栏的值设置为特定级别 .


有关可能有助于在搜索结果中显示此问题的另一个用例的更多信息,我想将alpha应用于我的资源中定义的颜色 .

使用@ sat的正确答案:

int alpha = ... // 0-255, calculated based on some business logic
int actionBarBackground = getResources().getColor(R.color.actionBarBackground);
int actionBarBackgroundWithAlpha = Color.argb(
        alpha,
        Color.red(actionbarBackground),
        Color.green(actionbarBackground),
        Color.blue(actionbarBackground)
);

6 回答

  • 0

    从非活动类访问颜色可能很困难 . 我找到的替代方案之一是使用 enum . enum 提供了很大的灵活性 .

    public enum Colors
    {
      COLOR0(0x26, 0x32, 0x38),    // R, G, B
      COLOR1(0xD8, 0x1B, 0x60),
      COLOR2(0xFF, 0xFF, 0x72),
      COLOR3(0x64, 0xDD, 0x17);
    
    
      private final int R;
      private final int G;
      private final int B;
    
      Colors(final int R, final int G, final int B)
      {
        this.R = R;
        this.G = G;
        this.B = B;
      }
    
      public int getColor()
      {
        return (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);
      }
    
      public int getR()
      {
        return R;
      }
    
      public int getG()
      {
        return G;
      }
    
      public int getB()
      {
        return B;
      }
    }
    
  • 770

    您可以使用:

    getResources().getColor(R.color.idname);
    

    在这里查看如何定义自定义颜色:

    http://sree.cc/google/android/defining-custom-colors-using-xml-in-android

    EDIT(1): 由于 getColor(int id) 现在为 deprecated ,因此必须使用:

    ContextCompat.getColor(context, R.color.your_color);
    

    (在支持库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
    
  • 103

    基于新的 Android Support Library (和this更新),现在您应该致电:

    ContextCompat.getColor(context, R.color.name.color);
    

    根据documentation

    public int getColor (int id)
    

    此方法在 API level 23 中已弃用 . 请改用getColor(int,Theme)

    它与 getResources().getColorStateList(id) 的解决方案相同:

    你必须改变它:

    ContextCompat.getColorStateList(getContext(),id);
    
  • 30

    定义颜色

    值/ 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>
    

    获取颜色int并进行设置

    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);
    

    另见

  • 0

    我更新使用 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));
        }
    }
    
  • 3

    最佳方法

    正如@sat的回答,获得色彩的好方法是

    ResourcesCompat.getColor(getResources(), R.color.your_color, null);
    

    当您无法访问 getResources() 方法时,请使用以下方法 .

    Context context  = getContext(); // like Dialog class
    ResourcesCompat.getColor(context.getResources(), R.color.your_color, null);
    

    我做的是什么

    public void someMethod(){
        ...
        ResourcesCompat.getColor(App.getRes(), R.color.your_color, null);
    }
    

    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()

    问题(当你没有上下文时)

    you don't have Context access 时,就像 Util 类中的方法一样 .

    假设没有Context的下面的方法 .

    public void someMethod(){
        ...
        // can't use getResource() without Context.
    }
    

    现在,您将在此方法中将 Context 作为参数传递并使用 getResources().

    public void someMethod(Context context){
        ...
        context.getResources...
    }
    

    所以这是一个 Bonus unique solution ,你可以通过它来访问像 Util class 这样的任何地方的资源 . 将 Resources 添加到 Application 类或如果不存在则创建一个 .

    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;
        }
    
    }
    

    将名称字段添加到 manifest.xml <application 标记 . (如果尚未添加)

    <application
            android:name=".App"
            ...
            >
            ...
        </application>
    

    现在你很高兴 . 在应用中的任何位置使用 ResourcesCompat.getColor(App.getRes(), R.color.your_color, null); .

相关问题