动态应用按钮样式

问题描述 投票:0回答:1

我有点麻烦:我将动态地应用@ 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);
    }
android button styles
1个回答
0
投票

如果您有XML风格

 ContextThemeWrapper newContext = new ContextThemeWrapper(baseContext, R.style.MyStyle);
    button = new Button(newContext);
© www.soinside.com 2019 - 2024. All rights reserved.