如何以编程方式仅将 android 主题更改为小吃店?

How to change an android theme programatically only to the snackbar?

我想更改我的自定义小吃店的主题

public class CustomSnackbar extends BaseTransientBottomBar<CustomSnackbar> {
  public CustomSnackbar(
      CoordinatorLayout parent,
      View contentView,
      BaseTransientBottomBar.ContentViewCallback contentViewCallback) {
    super(parent, contentView, contentViewCallback);
  }

我在布局中看到它可以设置为:

   android:theme="@style/Theme.my.WithDarkTextButton"

我试过从语法上设置主题,但没有找到对应的:

contentView.setTheme 缺失。每个 xml 属性不是可以通过代码设置吗?

如果您使用的是 LayoutInflater,则必须在 XML 中设置主题。

如果您以编程方式创建视图:

View contentView = new View(context);

然后稍微改一下:

View contentView = new View(new ContextThemeWrapper(context, R.style.Theme_my_WithDarkTextButton);

不能按需设置主题。 View在构造时只读取主题属性