how to get background color from current theme programmatically

前端 未结 3 1067
醉酒成梦
醉酒成梦 2020-12-02 17:02

I tried something like this, but i stuck:

TypedValue typedValue = new TypedValue(); 
if (this.parentActivity.getTheme().resolveAttribute(android.R.attr.windo         


        
3条回答
  •  再見小時候
    2020-12-02 17:54

    You can get the background color (or Drawable) from the current theme by:

    TypedValue a = new TypedValue();
    getTheme().resolveAttribute(android.R.attr.windowBackground, a, true);
    if (a.type >= TypedValue.TYPE_FIRST_COLOR_INT && a.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        // windowBackground is a color
        int color = a.data;
    } else {
        // windowBackground is not a color, probably a drawable
        Drawable d = activity.getResources().getDrawable(a.resourceId);
    }
    

提交回复
热议问题