2015-08-28 47 views
4

是否可以通过编程方式将Widget.AppCompat.Button主题应用于按钮?以编程方式将主题应用到按钮

Button button = new Button(context); 
button.setText("Button"); 

目前,我使用的是试图实现像程序兼容性主题风格的自定义绘制资源,但我认为这可能是更容易实现。

在布局它实现这样的:提前

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeBasedOnAppcompatButton"/> 

感谢。

回答

10

您应该使用ContextThemeWrapper,它会更改默认主题。刚刚得到一个背景下,你通常会,并用它象下面这样:

new Button(new ContextThemeWrapper(context, R.style.my_theme)); 
+0

谢谢!我想我的方法来实现动态布局是错误的。我会用listview适配器替换它。 –

0

如果您需要progamatically更换主题和您使用的是支持库,你可以简单地使用:

TextViewCompat.setTextAppearance(getContext(), R.style.AppTheme_TextStyle_ButtonDefault_Whatever); 
用于TextViews和Buttons的

。其余的视图有类似的类:-)

相关问题