以编程方式设置TextView的样式

我试图使用TextView构造函数,样式如下:

TextView myText = new TextView(MyActivity.this, null, R.style.my_style);

但是,当我这样做时,文本视图似乎没有采用该样式(我通过在静态对象上设置该样式来验证该样式)。

我也尝试过使用myText.setTextAppearance(MyActivity.this, R.style.my_style),但它也不工作。

211511 次浏览

我不相信您可以通过编程方式设置样式。为了解决这个问题,你可以创建一个带有指定样式的模板布局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);

不支持动态更改样式。你必须通过XML设置样式之前来创建视图。

我只使用EditText进行了测试,但您可以使用该方法

setBackgroundResource (int residual)

来应用XML文件中定义的样式。

因为这个方法属于视图,我相信它将适用于任何UI元素。

的问候。

你可以创建一个通用样式,并在多个文本视图上重复使用,如下图所示:

textView.setTextAppearance(this, R.style.MyTextStyle);

编辑:this指向Context对象。

您可以在构造函数中设置样式(但样式不能动态更改/设置)。

View(Context, AttributeSet, int) (int是当前主题中的属性,包含对样式的引用)

Romain Guy的回答

reference .

你可以像这样将ContextThemeWrapper传递给构造函数:

TextView myText = new TextView(new ContextThemeWrapper(MyActivity.this, R.style.my_style));

当使用可能使用样式继承(或事件可样式属性)的自定义视图时,您必须修改第二个构造函数,以免丢失样式。这对我来说是有效的,不需要使用setTextAppearence ():

public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, attrs.getStyleAttribute());
}

这个公认的答案对我来说是一个很好的解决方案。唯一要添加的是inflate()方法。

在已接受的回答中,所有android:layout_*参数将不会被应用。

原因是没有办法调整它,因为null被传递为ViewGroup parent

你可以这样使用它:

View view = inflater.inflate(R.layout.view, parent, false);

parentViewGroup,从这里你可以调整android:layout_*

在这种情况下,将设置所有相关属性。

希望对别人有用。

我也遇到过这个问题,我找到了以编程方式设置样式的方法。也许你们都需要,所以我在这里更新。

视图构造函数的第三个参数接受主题中的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 -当前主题中的属性 样式资源的引用,该样式资源为 视图。

要在View构造函数中设置样式,我们有两个可能的解决方案:

  1. 使用ContextThemeWrapper:

    ContextThemeWrapper wrappedContext = new ContextThemeWrapper(yourContext, R.style.your_style);
    TextView textView = new TextView(wrappedContext, null, 0);
    
  2. With four-argument constructor (available starting from LOLLIPOP):

    TextView textView = new TextView(yourContext, null, 0, R.style.your_style);
    

Key thing for both solutions - defStyleAttr parameter should be 0 to apply our style to the view.

我们可以使用TextViewCompact.setTextAppearance(textView, R.style.xyz)

Android 医生供参考。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
textView.setTextAppearance(R.style.yourStyle)

你可以使用kotlin扩展函数

fun TextView.setStyle(@StyleRes resId: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
setTextAppearance(resId)
} else {
setTextAppearance(context, resId)
}
}