Is there such a method call “getBackgroundColor”?

前端 未结 6 1778
旧巷少年郎
旧巷少年郎 2021-01-17 07:29

Is there such a method call \"getBackgroundColor\" in TextView? if I got 2 textViews: tv1 and tv2 in one LinearLayout. What I did:tv1.setBackgroundColor(Color.BLUE)

6条回答
  •  伪装坚强ぢ
    2021-01-17 07:57

    It works for me.

    public static int getColor(View v) {
        if(Build.VERSION.SDK_INT>=11)
        {
            return ((ColorDrawable)v.getBackground()).getColor();
        }
        else
        {
           try
           {
            Field f=View.class.getDeclaredField("mState");
            f.setAccessible(true);
            Object mState=f.get(v);
            Field f2=mState.getClass().getDeclaredField("mUseColor");
            f2.setAccessible(true);
            return (int) f2.get(mState);
           }
           catch (Exception e)
           {
    
           }
        }
        return 0;
    }
    

提交回复
热议问题