Android 如何扩展按钮的样式?

Android 如何扩展按钮的样式?,android,Android,我正在活动中创建一个按钮: Button contactButton = new Button(this, null, R.style.ContactButton); contactButton.setText(contactName); contactsView.addView(contactButton); 具有相应的样式: <style name="ContactButton" parent="@android:style/Widget.Button"> </styl

我正在活动中创建一个按钮:

Button contactButton = new Button(this, null, R.style.ContactButton);
contactButton.setText(contactName);
contactsView.addView(contactButton);
具有相应的样式:

<style name="ContactButton" parent="@android:style/Widget.Button"> 
</style>
那么,有什么办法可以做到这一点吗

更新:

这似乎是一个,这解释了为什么
new按钮(这个,null,android.R.attr.buttonStyle)
有效,而
new按钮(这个,null,android.R.style.Widget_按钮)
无效,尽管前者只是后者的参考

我之所以会遇到这个错误,是因为我是通过编程方式创建按钮的,在XML中设置
style
属性就可以了


我仍然想知道是否有一个简单的解决方法仍然允许我使用样式系统。

能否创建一个factory类并让它返回重新定义对象的新实例

Class ButtonFactory(){
    public Button getStyledButton(String name){
        Button styledButton = new Button();
        styledButton.setText(name);
        // do stuff to button
        return styledButton
    }
}
将此添加到您的活动中

onCreate(..){
   ButtonFactory builder = new ButtonFactory();
   Button styledButton = builder.getStyledButton();
}

是的,直接修改按钮实例确实有效,但我希望使用样式系统将其很好地分开。您是否尝试过从XML中扩展按钮,而不是实用地创建它?您找到解决方案了吗?
onCreate(..){
   ButtonFactory builder = new ButtonFactory();
   Button styledButton = builder.getStyledButton();
}