android 怎么动态设置button 的style

网上找了很多,还是没有直接的解决办法,button没有setstyle这个方法。因此我的解决办法如下:

直接动态设置各个属性

Button themeBtn = new Button(this);
            themeBtn.setText(entry.getKey());
            LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
            themeBtn.setLayoutParams(lparams);
            themeBtn.setTextAppearance(this, R.style.ImageCategoryThemeBtn);
            themeBtn.setPadding(DensityUtils.dp2px(this, 8), 0, DensityUtils.dp2px(this, 8), 0);
            themeBtn.setBackgroundResource(R.drawable.selector_bt_gray2blue);
            v_theme.addView(themeBtn);

style:

<style name="ImageCategoryThemeBtn" parent="@android:style/Widget.Button">
        <item name="android:textColor">@color/image_theme</item>
        <item name="android:textSize">@dimen/imageThemeBtnTextSize</item>
    </style>
原文地址:https://www.cnblogs.com/feijian/p/4531333.html