动态应用按钮样式

Apply button style dynamically

我有点小麻烦: 我将动态创建应用@style/Widget.AppCompat.Button.Borderless 样式的material 按钮。显然,如果我在 xml 文件中将按钮创建为静态按钮,它工作正常,但在 java 代码中我不知道该怎么做。这是我的代码

  for (String nameCat : cat){
        Button button = new Button(getActivity());
        lP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dpToPx(60)); // dpToPx is convert method
        button.setLayoutParams(lP);

        // text button
        button.setText(nameCat);
        // add to linear layout
        lL.addView(button);
    }

如果您有自己的风格 XML

 ContextThemeWrapper newContext = new ContextThemeWrapper(baseContext, R.style.MyStyle);
    button = new Button(newContext);