【问题标题】:Get the background color of a button in android获取android中按钮的背景颜色
【发布时间】:2011-12-26 16:56:23
【问题描述】:

如何获取按钮的背景颜色。 在 xml 中,我使用 ---- android:background = XXXXX 设置背景颜色 现在在活动类中我如何检索它具有的这个值?

【问题讨论】:

    标签: android button background-color


    【解决方案1】:

    很遗憾,我不知道如何检索实际颜色。

    Drawable 很容易得到它

    Button button = (Button) findViewById(R.id.my_button);
    Drawable buttonBackground = button.getBackground();
    

    如果你知道这是一种颜色,那么你可以试试

    ColorDrawable buttonColor = (ColorDrawable) button.getBackground();
    

    如果您使用的是 Android 3.0+,则可以获取颜色的资源 ID。

    int colorId = buttonColor.getColor();
    

    并将其与您分配的颜色进行比较,即。

    if (colorID == R.color.green) {
      log("color is green");
    }
    

    【讨论】:

    • 你确定 getColor() 得到了 id 吗?我认为它获得了颜色的实际 int 值(即 0xAARRGGBB)。我用“#00000001”对此进行了测试,它返回了 1。
    • 在我的测试中我做了button.setBackground(R.color.green) 然后检查了响应,它肯定不是实际的颜色ID。我更喜欢一个合适的颜色整数,所以我可以Color.red(int)Color.green(int)Color.blue(int) 它。但在我对 Android 3.2 的测试中,情况并非如此。我猜这是不一致的,根据上下文返回颜色 int 或残差。
    • 嘿,我正在尝试仅当图像按钮的背景图像是某个可绘制资源时才执行任务。我该如何比较...我试过 if(buttonBackground.equals(@drawable/mydrawable)) 但它不起作用
    • 我用这个((ColorDrawable) row.getBackground()).getColor() 作为(row.background as ColorDrawable).color 但我遇到了这个错误android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.ColorDrawable
    【解决方案2】:
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Rect mBounds;
    
    public void initIfNeeded() {
      if(mBitmap == null) {
        mBitmap = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        mBounds = new Rect();
      }
    }
    
    public int getBackgroundColor(View view) {
      // The actual color, not the id.
      int color = Color.BLACK;
    
      if(view.getBackground() instanceof ColorDrawable) {
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
          initIfNeeded();
    
          // If the ColorDrawable makes use of its bounds in the draw method,
          // we may not be able to get the color we want. This is not the usual
          // case before Ice Cream Sandwich (4.0.1 r1).
          // Yet, we change the bounds temporarily, just to be sure that we are
          // successful.
          ColorDrawable colorDrawable = (ColorDrawable)view.getBackground();
    
          mBounds.set(colorDrawable.getBounds()); // Save the original bounds.
          colorDrawable.setBounds(0, 0, 1, 1); // Change the bounds.
    
          colorDrawable.draw(mCanvas);
          color = mBitmap.getPixel(0, 0);
    
          colorDrawable.setBounds(mBounds); // Restore the original bounds.
        }
        else {
          color = ((ColorDrawable)view.getBackground()).getColor();
        }
      }
    
      return color;
    }
    

    【讨论】:

    • 我将这个((ColorDrawable) row.getBackground()).getColor() 用作(row.background as ColorDrawable).color,但我遇到了这个错误android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.ColorDrawable
    • 您不应该将row.getBackground() 转换为ColorDrawable 而不先检查它是否确实是ColorDrawable。这就是我从if(view.getBackground() instanceof ColorDrawable) 开始的原因。对于StateListDrawableDrawableContainer),您可能需要使用drawable.getCurrent()。然而,仅此可能还不够。这实际上取决于drawable的结构。另外,请注意,这个 sn-p 已经有将近 7 年了,从那时起发生了很多变化。祝你好运。
    【解决方案3】:

    您也可以尝试将颜色值设置为标签,例如

    android:tag="#ff0000"
    

    并从代码中访问它

    String colorCode = (String)btn.getTag();
    

    【讨论】:

    • 我认为这是最好的方法,也可以:@color/your_color,或者,?attr/colorPrimary
    【解决方案4】:

    为我获取颜色的最简单方法是:

    int color = ((ColorDrawable)button.getBackground()).getColor();
    

    在 Lollipop 5.1.1 上测试和工作

    【讨论】:

      【解决方案5】:

      要获取背景Drawable,请使用

      public Drawable getBackground();
      

      在基类 View 中定义。

      不要忘记Button 可以具有图像、颜色、渐变的背景。如果使用 android:background="#ffffff",则背景的类为

      android.graphics.drawable.ColorDrawable

      从那里你可以简单地调用

      public int getColor()
      

      【讨论】:

        【解决方案6】:

        试试这个:

        list_view.getChildAt(position).setBackgroundColor(Color.YELLOW);        
        ColorDrawable corItem = (ColorDrawable) list_view.getChildAt(position).getBackground();
        if(corItem.getColor() == Color.YELLOW){
           Toast.makeText(NovoProcessoActivity.this,"Right Color!", Toast.LENGTH_SHORT).show();
           }else{
           Toast.makeText(NovoProcessoActivity.this,"Wrong Color!", Toast.LENGTH_SHORT).show();
        }
        

        int color =( (ColorDrawable)  list_view.getChildAt(position).getBackground()).getColor();
        

        【讨论】:

          【解决方案7】:
          int colornumber=((ColorDrawable)v.getBackground()).getColor();
          

          这是获取视图(按钮、文本视图...)颜色的最佳且最简单的方法

          要使用 java 为视图设置颜色,我们使用

          v.setBackgroundColor(getColor(R.color.colorname_you_used));
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-08-06
            • 1970-01-01
            • 2022-01-02
            • 2012-04-07
            • 2016-08-12
            • 1970-01-01
            • 2017-11-05
            • 2013-06-24
            相关资源
            最近更新 更多