6

我需要动态创建一个样式按钮。我想也许我应该这样做:

XmlPullParser parser = m_context.getResources().getXml(R.style.Button_Plain);
buttonStyle = Xml.asAttributeSet(parser);
Button btn = new Button (m_context, buttonStyle);

getXml抛出异常“请求资源失败,因为它很复杂”。有什么简单的方法可以做我需要的吗?

4

2 回答 2

7

使用以下构造函数创建按钮对象:

http://developer.android.com/reference/android/widget/Button.html#Button(android.content.Context , android.util.AttributeSet, int)

public Button (Context context, AttributeSet attrs, int defStyle)

并传递以下参数:

Button btn = new Button (m_context, null, R.style.Button_Plain);

无需使用 XmlPullParser。

于 2012-07-25T12:03:37.453 回答
0

它基本上是这样的:

Button button = new Button(ContextActivity, null, R.style.whateverStyleYouHad);

于 2017-06-12T07:24:40.797 回答