【问题标题】:Android - set TextView TextStyle programmatically?Android - 以编程方式设置 TextView TextStyle?
【发布时间】:2011-12-16 16:16:30
【问题描述】:

有没有办法以编程方式设置TextViewtextStyle 属性?似乎没有setTextStyle() 方法。

要清楚,我不是在谈论 View / Widget 样式!我说的是以下内容:

<TextView
  android:id="@+id/my_text"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Hello World"
  android:textStyle="bold" />

【问题讨论】:

标签: java android textview


【解决方案1】:
textview.setTypeface(Typeface.DEFAULT_BOLD);

setTypeface 是属性 textStyle。

正如 Shankar V 添加的那样,为了保留之前设置的字体属性,您可以使用:

textview.setTypeface(textview.getTypeface(), Typeface.BOLD);

【讨论】:

  • 应该是textview.setTypeface(textview.getTypeface(), Typeface.DEFAULT_BOLD);
  • @IlyaEremin 保留之前设置的字体,您需要像这样使用它。
  • 根据@SankarV 评论编辑了我的答案。
  • 我认为你应该放弃默认值:holder.title.setTypeface(holder.title.getTypeface(), Typeface.BOLD);
  • 其实textview.getTypeface().getStyle()就是android:textStyle
【解决方案2】:

搜索setTextAppearancesetTextTypeface。 stackoverflow 上有类似的问题:How to change a TextView's style at runtime

【讨论】:

  • setTextAppearance 已弃用 api 级别 23
  • api 23+ 有一个新方法,它只是不接受上下文参数。
【解决方案3】:

here 所述,目前不支持此功能。

【讨论】:

    【解决方案4】:

    假设您的 values/styles.xml 中有一个名为 RedHUGEText 的样式:

    <style name="RedHUGEText" parent="@android:style/Widget.TextView">
        <item name="android:textSize">@dimen/text_size_huge</item>
        <item name="android:textColor">@color/red</item>
        <item name="android:textStyle">bold</item>
    </style>
    

    只需像往常一样在 XML 布局/your_layout.xml 文件中创建您的 TextView,假设:

    <TextView android:id="@+id/text_view_title" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content 
        android:text="FOO" />
    

    在你的 Activity 的 java 代码中你这样做:

    TextView textViewTitle = (TextView) findViewById(R.id.text_view_title);
    textViewTitle.setTextAppearance(this, R.style.RedHUGEText);
    

    它对我有用!它应用了颜色、大小、重力等。我已经在 Android API 级别从 8 到 17 的手机和平板电脑上使用它,没有任何问题。请注意,从 Android 23 开始,该方法已被弃用。上下文参数已被删除,所以最后一行需要是:

    textViewTitle.setTextAppearance(R.style.RedHUGEText);
    

    要支持所有 API 级别,请使用 androidX TextViewCompat

    TextViewCompat.setTextAppearance(textViewTitle, R.style.RedHUGEText)
    

    请记住...仅当文本的样式确实取决于 Java 逻辑上的条件或者您正在使用代码“动态”构建 UI 时才有用...如果不是,则最好这样做:

    <TextView android:id="@+id/text_view_title" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content 
        android:text="FOO" 
        style="@style/RedHUGEText" />
    

    你可以随心所欲!

    【讨论】:

    • 这只会设置可以在&lt;style&gt; 标签中定义的属性子集。它执行常见的(大小、颜色、样式),但不应用其他的(包括填充、背景、重力等)
    • 似乎要求最低 API 23?
    • 有两种方法,一种用于api
    【解决方案5】:

    我用两种简单的方法解决了这个问题。

    按照说明进行。

    我现有的样式声明:

    <style name="SearchInfoText">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textSize">24sp</item>
        <item name="android:textColor">@color/Church_Grey</item>
        <item name="android:shadowColor">@color/Shadow_Church</item>
        <item name="android:shadowRadius">3</item>
        <item name="android:shadowDx">1</item>
        <item name="android:shadowDy">1</item>
    </style>
    

    我的 Android Java 代码:

        TextView locationName = new TextView(getSupportActivity());
        locationName.setId(IdGenerator.generateViewId());
        locationName.setText(location.getName());
        locationName.setLayoutParams(super.centerHorizontal());
        locationName.setTextSize(24f);
        locationName.setPadding(0, 0, 0, 15);
        locationName.setTextColor(getResources().getColor(R.color.Church_Grey));
        locationName.setShadowLayer(3, 1, 1,  getResources().getColor(R.color.Shadow_Church));
    

    问候。

    【讨论】:

      【解决方案6】:

      完成这项任务的方法有很多,如下所示:-

      1.

      String text_view_str = "<b>Bolded text</b>, <i>italic text</i>, even <u>underlined</u>!";
      TextView tv = (TextView)findViewById(R.id.ur_text_view_id);
      tv.setText(Html.fromHtml(text_view_str));
      

      2.

      tv.setTypeface(null, Typeface.BOLD);
      tv.setTypeface(null, Typeface.ITALIC);
      tv.setTypeface(null, Typeface.BOLD_ITALIC);
      tv.setTypeface(null, Typeface.NORMAL);
      

      3.

      SpannableString spannablecontent=new SpannableString(o.content.toString());
      spannablecontent.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 
                               0,spannablecontent.length(), 0);
      // set Text here
      tt.setText(spannablecontent);
      

      4.

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
      
          <style name="boldText">
              <item name="android:textStyle">bold|italic</item>
              <item name="android:textColor">#FFFFFF</item>
          </style>
      
          <style name="normalText">
              <item name="android:textStyle">normal</item>
              <item name="android:textColor">#C0C0C0</item>
          </style>
      
      </resources>
      
       tv.setTextAppearance(getApplicationContext(), R.style.boldText);
      

      或者如果你想通过 xml

      android:textStyle="normal"
      android:textStyle="normal|bold"
      android:textStyle="normal|italic"
      android:textStyle="bold"
      android:textStyle="bold|italic"
      

      【讨论】:

      • setTextAppearance() 是我为了应用在我的 XML 中定义的自定义样式而寻求的答案。谢谢。
      • 在我看来,这是该主题的最佳答案。以编程方式调用代码的许多选项
      【解决方案7】:

      这个问题在很多地方以很多不同的方式提出。我最初在这里回答了它,但我觉得它在这个线程中也很相关(因为我在寻找答案时最终选择了here)。

      这个问题没有单一的解决方案,但这适用于我的用例。问题是,'View(context, attrs, defStyle)' 构造函数不引用实际样式,它需要一个属性。所以,我们将:

      1. 定义一个属性
      2. 创建您想要使用的样式
      3. 在我们的主题上应用该属性的样式
      4. 使用该属性创建视图的新实例

      在'res/values/attrs.xml'中,定义一个新属性:

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
          <attr name="customTextViewStyle" format="reference"/>
          ...
      </resources>    
      

      在 res/values/styles.xml' 我将创建我想在我的自定义 TextView 上使用的样式

      <style name="CustomTextView">
          <item name="android:textSize">18sp</item>
          <item name="android:textColor">@color/white</item>
          <item name="android:paddingLeft">14dp</item>
      </style>
      

      在 'res/values/themes.xml' 或 'res/values/styles.xml' 中,为您的应用程序/活动修改主题并添加以下样式:

      <resources>
          <style name="AppBaseTheme" parent="android:Theme.Light">
              <item name="@attr/customTextViewStyle">@style/CustomTextView</item>
          </style>
          ... 
      </resources>
      

      最后,在您的自定义 TextView 中,您现在可以使用带有属性的构造函数,它将接收您的样式

      public class CustomTextView extends TextView {
      
          public CustomTextView(Context context) {
             super(context, null, R.attr.customTextView);
          }
      }
      

      值得注意的是,我在不同的变体和不同的地方反复使用了customTextView,但绝不要求视图的名称与样式或属性或任何内容匹配。此外,这种技术应该适用于任何自定义视图,而不仅仅是 TextViews。

      【讨论】:

        【解决方案8】:

        这对我有用

        textview.setTypeface(textview.getTypeface(), Typeface.BOLD);
        

        textview.setTypeface(Typeface.DEFAULT_BOLD);
        

        【讨论】:

          【解决方案9】:

          Kotlin Version

          在文本样式之外保留当前字体:

          textView.apply {
              setTypeface(typeface, Typeface.NORMAL)
              // or
              setTypeface(typeface, Typeface.BOLD)
              // or
              setTypeface(typeface, Typeface.ITALIC)
              // or
              setTypeface(typeface, Typeface.BOLD_ITALIC)
          }
          

          【讨论】:

            【解决方案10】:

            你可以试试这个

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            textView.setTextAppearance(R.style.Lato_Bold);
                        } else {
                            textView.setTextAppearance(getActivity(), R.style.Lato_Bold);
                        }
            

            【讨论】:

              【解决方案11】:

              由于setTextAppearance(resId) 仅适用于 API 23 及更高版本,请使用:

              TextViewCompat.setTextAppearance(textViewGoesHere, resId)

              该方法内部实现如下:

              public static void setTextAppearance(@NonNull TextView textView, @StyleRes int resId) {
                  if (Build.VERSION.SDK_INT >= 23) {
                      textView.setTextAppearance(resId);
                  } else {
                      textView.setTextAppearance(textView.getContext(), resId);
                  }
              }
              

              【讨论】:

                【解决方案12】:

                这对我有用

                msg.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                OR
                TextView.setTypeface(msg.getTypeface(),Typeface.NORMAL);
                

                改为关闭

                TextView.setTypeface(Typeface.DEFAULT_BOLD);
                

                【讨论】:

                  猜你喜欢
                  • 2019-04-10
                  • 2011-03-09
                  • 1970-01-01
                  • 2011-01-28
                  • 2018-02-02
                  • 1970-01-01
                  • 2017-07-09
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多