【问题标题】:How do I set the style of a textView programmatically?如何以编程方式设置 textView 的样式?
【发布时间】:2013-02-07 19:59:28
【问题描述】:

我正在以编程方式创建一个 textView。有没有办法可以设置这个 textView 的样式?类似于

style="@android:style/TextAppearance.DeviceDefault.Small"

如果我有 layout.xml 文件,我会使用它。

【问题讨论】:

标签: android styles textview android-styles


【解决方案1】:

您无法以编程方式设置视图的样式,但您可以执行textView.setTextAppearance(context, android.R.style.TextAppearance_Small); 之类的操作。

【讨论】:

  • 使用if (Build.VERSION.SDK_INT < 23) { textView.setTextAppearance(context, android.R.style.TextAppearance_Small); } else { textView.setTextAppearance(android.R.style.TextAppearance_Small); }
【解决方案2】:

目前无法以编程方式设置视图的样式。

要解决这个问题,您可以创建一个指定样式的模板布局 xml 文件,例如在 res/layout create 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="@android:style/TextAppearance.DeviceDefault.Small" />

然后将其膨胀以实例化您的新 TextView:

TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);

【讨论】:

  • 我比接受的答案更喜欢这个解决方案,因为它支持 OP 要求的样式(我想要的)。此外,使用 android.widget 构造函数总是感觉如此....错误....
【解决方案3】:

实际上,从 API 级别 21 开始,这是可能的。

TextView 有一个4 parameter constructor:

TextView (Context context, 
          AttributeSet attrs, 
          int defStyleAttr, 
          int defStyleRes)

在这种情况下,中间两个参数不是必需的。下面的代码直接在activity中创建了一个TextView,并且只定义了它的样式资源:

TextView myStyledTextView = new TextView(this, null, 0, R.style.my_style);

【讨论】:

    【解决方案4】:

    兼容版本(因为 api 23 已弃用):

    TextViewCompat.setTextAppearance(textView, android.R.style.TextAppearance_DeviceDefault_Small);
    

    【讨论】:

      【解决方案5】:

      试试这个

      textview.setTextAppearance(context, R.style.yourstyle);
      

      这可能不起作用尝试使用这样的 textview 创建一个 xml

      textviewstyle.xml
      
      <TextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              style="@android:style/TextAppearance.DeviceDefault.Small" />
      

      要获得所需的样式,请对包含 textview 的 xml 进行扩展

      TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvstyle, null);
      

      【讨论】:

        【解决方案6】:

        对于在运行时(在代码中)生成的视图,您可以将样式传递给构造函数:View(Context, AttributeSet, int)

        在其他情况下,您必须调用各种方法来更改外观

        【讨论】:

          【解决方案7】:
          @Deprecated
          public void setTextAppearance(Context context, @StyleRes int resId)
          

          此方法自 Android SDK 23 起已弃用。

          您可以使用安全版本:

          if (Build.VERSION.SDK_INT < 23) {
              super.setTextAppearance(context, resId);
          } else {
              super.setTextAppearance(resId);
          }
          

          【讨论】:

            【解决方案8】:
            textView.setTextAppearance(context, android.R.style.TextAppearance_Small);
            

            在 java 中已被弃用,所以在没有上下文的情况下使用它用于 kotlin

            textView.setTextAppearance(android.R.style.TextAppearance_Small)
            

            【讨论】:

              猜你喜欢
              • 2011-03-09
              • 1970-01-01
              • 2017-10-20
              • 1970-01-01
              • 2019-03-27
              • 2011-12-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多