我有点麻烦:我将动态地应用@ style / Widget.AppCompat.Button.Borderless样式的材质按钮。显然,如果我在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);