“You need to use a Theme.AppCompat theme (or descendant) with the design library” error

后端 未结 2 1210
逝去的感伤
逝去的感伤 2020-11-28 15:09

I\'m getting \"You need to use a Theme.AppCompat theme (or descendant) with the design library\" error every time even if I\'m obviously using an AppCompat Theme (a descenda

相关标签:
2条回答
  • 2020-11-28 15:16

    Create a ContextThemeWrapper to wrap the Service's Context with your custom theme, and get the LayoutInflater from that.

    ContextThemeWrapper ctx = new ContextThemeWrapper(this, R.style.TranslucentAppTheme);
    tooltipContainer = (CoordinatorLayout) LayoutInflater.from(ctx)
        .inflate(R.layout.tooltip_layout, null);
    

    The ContextThemeWrapper modifies the given Context's theme with the one you specify in the constructor. Since a Service doesn't really have a theme, it just tacks yours onto the Service's Context, then the LayoutInflater has the appropriate theme to inflate the design Views.

    0 讨论(0)
  • 2020-11-28 15:38

    Also receive such error in logcat:

    "E/ThemeUtils: View class TableCircleCustomView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant)."

    My app theme is "Theme.AppCompat.Light.NoActionBar"

    Here is my custom view class:

    class TableCircleCustomView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
    ) : AppCompatTextView(context, attrs, defStyleAttr) {
        ....
    }
    

    Then in Fragment i'm adding this object to RelativeLayout like so

    val viewItem = TableCircleCustomView(context)     
    with(RelativeLayout.LayoutParams(objectWidth, objectHeight)) {
      leftMargin = objectPosX
      topMargin = objectPosY
        
      binding.restaurantMap.addView(viewItem, this);
    }
    

    Can somebody help to solve it. Have no idea for what element i should set theme and how ((

    0 讨论(0)
提交回复
热议问题