android在代码中设置样式
我试图使用这样的样式的TextView构造函数:
TextView myText = new TextView(MyActivity.this, null, R.style.my_style ); 
但是,当我这样做时,文本视图不会采取样式(我通过将其设置在静态对象上验证样式)。
 我也尝试使用myText.setTextAppearance(MyActivity.this, R.style.my_style)但它也不起作用 
我不相信你可以以编程方式设置风格。 为了解决这个问题,可以使用指定的样式创建一个模板布局xml文件,例如在res / layout中创建tvtemplate.xml,如下所示:
 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is a template" style="@style/my_style" /> 
然后膨胀这个来实例化你的新的TextView:
 TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null); 
希望这可以帮助。
您可以创建一个通用样式,并在多个文本视图中重新使用它,如下所示:
 textView.setTextAppearance(this, R.style.MyTextStyle); 
编辑: 这是指上下文
您可以将ContextThemeWrapper传递给构造函数,如下所示:
 TextView myText = new TextView(new ContextThemeWrapper(MyActivity.this, R.style.my_style)); 
您可以在构造函数中设置样式(但不能动态更改/设置样式)。
  View(Context, AttributeSet, int) ( int是样式资源) 
罗曼盖伊回答
参考
动态改变样式不支持(还)。 您必须在视图创建之前通过XML来设置样式。
 接受的答案对我来说是很好的解决方案。 唯一要添加的是关于inflate()方法。 
 在接受的答案中,所有android:layout_*参数将不会被应用。 
 原因是无法调整它,导致null作为ViewGroup parent传递。 
你可以像这样使用它:
 View view = inflater.inflate(R.layout.view, parent, false); 
  parent是ViewGroup ,从你喜欢调整android:layout_* 。 
在这种情况下,所有相关属性将被设置。
希望这会对某人有用。
使用可能使用样式继承(或事件样式属性)的自定义视图时,您必须修改第二个构造函数以避免丢失样式。 这为我工作,无需使用setTextAppearence() :
 public CustomView(Context context, AttributeSet attrs) { this(context, attrs, attrs.getStyleAttribute()); } 
我也遇到了这个问题,我找到了编程方式来设置风格。 也许你们都需要它,所以我在那里更新。
视图构造函数的第三个参数在你的主题中接受一个类型的attr作为源代码如下:
 public TextView(Context context, AttributeSet attrs) { this(context, attrs, com.android.internal.R.attr.textViewStyle); } 
所以你必须通过一种R.attr。**而不是R.style。**
在我的代码中,我做了以下步骤:
首先,定制一个自定义的attr以供attr.xml中的主题使用。
 <attr name="radio_button_style" format="reference" /> 
其次,在style.xml中使用的主题中指定你的风格。
  <style name="AppTheme" parent="android:Theme.Translucent"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> <item name="radio_button_style">@style/radioButtonStyle</item> </style> <style name="radioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">64dp</item> <item name="android:background">#000</item> <item name="android:button">@null</item> <item name="android:gravity">center</item> <item name="android:saveEnabled">false</item> <item name="android:textColor">@drawable/option_text_color</item> <item name="android:textSize">9sp</item> </style> 
最后,使用它!
  RadioButton radioButton = new RadioButton(mContext, null, R.attr.radio_button_style); 
以编程方式创建的视图将在您的主题中使用指定的样式。
你可以尝试一下,希望它能为你完美地工作。
 参数int defStyleAttr没有指定样式。 从Android文档: 
defStyleAttr – 当前主题中包含对为视图提供默认值的样式资源的引用的属性。 可以是0来不寻找默认值。
要在View构造函数中设置样式,我们有两个可能的解决方案:
- 
使用ContextThemeWrapper: ContextThemeWrapper wrappedContext = new ContextThemeWrapper(yourContext, R.style.your_style); TextView textView = new TextView(wrappedContext, null, 0);
- 
用四个参数的构造函数(从LOLLIPOP开始): TextView textView = new TextView(yourContext, null, 0, R.style.your_style);
 这两个解决方案的关键是 – defStyleAttr参数应该是0,将我们的风格应用到视图。 
我只用EditText进行了测试,但是可以使用该方法
public void setBackgroundResource(int resid)
应用XML文件中定义的样式。
正弦这个方法属于视图我相信它可以与任何UI元素一起工作。
问候。