Android - 以编程方式设置TextView TextStyle?

Jak*_*son 212 java android textview

有没有办法以编程方式设置textStyle属性TextView?似乎没有一种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" />
Run Code Online (Sandbox Code Playgroud)

Raz*_*Raz 373

textview.setTypeface(Typeface.DEFAULT_BOLD);
Run Code Online (Sandbox Code Playgroud)

setTypeface是Attribute textStyle.

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

textview.setTypeface(textview.getTypeface(), Typeface.BOLD);
Run Code Online (Sandbox Code Playgroud)

  • 它应该是`textview.setTypeface(textview.getTypeface(),Typeface.DEFAULT_BOLD);` (14认同)
  • 我认为你应该放弃DEFAULT:holder.title.setTypeface(holder.title.getTypeface(),Typeface.BOLD); (13认同)
  • @IlyaEremin保存以前设置的字体,你需要像这样使用它. (2认同)

Osc*_* S. 115

假设您在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>
Run Code Online (Sandbox Code Playgroud)

只需像往常一样在XML layout/your_layout.xml文件中创建TextView,让我们说:

<TextView android:id="@+id/text_view_title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content 
    android:text="FOO" />
Run Code Online (Sandbox Code Playgroud)

在您的Activity的java代码中,您执行以下操作:

TextView textViewTitle = (TextView) findViewById(R.id.text_view_title);
textViewTitle.setTextAppearance(this, R.style.RedHUGEText);
Run Code Online (Sandbox Code Playgroud)

它对我有用!它应用了颜色,大小,重力等.我已经在Android API级别从8到17的手机和平板电脑上使用它,没有任何问题.

请记住......只有当文本的样式真正依赖于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" />
Run Code Online (Sandbox Code Playgroud)

你可以随时随地拥有它!

希望能帮助到你!

  • 这只会设置一个可以在`<style>`标签中定义的属性子集.它执行常见的(大小,颜色,样式),但不适用于其他(包括填充,背景,重力等) (4认同)
  • 似乎需要min API 23? (3认同)
  • 有两种方法,一种用于api <23,一种用于api 23+.它们看起来完全相同,除了<23采用上下文参数而23 +不采用上下文参数.在引擎盖下,api 23+的方法调用方法<23并使用成员上下文进行textview. (3认同)

pet*_*tos 62

搜索setTextAppearance或也setTextTypeface.stackoverflow上有类似的问题:如何在运行时更改TextView的样式


dug*_*ggu 50

实现这一任务的方法有很多,如下:

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));
Run Code Online (Sandbox Code Playgroud)

2.

tv.setTypeface(null, Typeface.BOLD);
tv.setTypeface(null, Typeface.ITALIC);
tv.setTypeface(null, Typeface.BOLD_ITALIC);
tv.setTypeface(null, Typeface.NORMAL);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

或者如果你想通过xml

android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
Run Code Online (Sandbox Code Playgroud)


Kev*_*ant 7

很多地方都会以很多不同的方式提出这个问题.我最初在这里回答它,但我觉得它在这个帖子中也是相关的(因为我在搜索答案时最终到了这里).

这个问题没有一线解决方案,但这适用于我的用例.问题是,'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>    
Run Code Online (Sandbox Code Playgroud)

在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>
Run Code Online (Sandbox Code Playgroud)

在'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>
Run Code Online (Sandbox Code Playgroud)

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

public class CustomTextView extends TextView {

    public CustomTextView(Context context) {
       super(context, null, R.attr.customTextView);
    }
}
Run Code Online (Sandbox Code Playgroud)

值得注意的是,我在不同的变体和不同的地方重复使用customTextView,但是视图的名称与样式或属性或任何东西都不匹配.此外,此技术应该适用于任何自定义视图,而不仅仅是TextViews.


Ray*_*ter 6

这对我有用

textview.setTypeface(textview.getTypeface(), Typeface.BOLD);
Run Code Online (Sandbox Code Playgroud)

或者

textview.setTypeface(Typeface.DEFAULT_BOLD);
Run Code Online (Sandbox Code Playgroud)


ami*_*phy 6

Kotlin Version

要保留除文字样式外的当前字体:

textView.apply {
    setTypeface(typeface, Typeface.NORMAL)
    // or
    setTypeface(typeface, Typeface.BOLD)
    // or
    setTypeface(typeface, Typeface.ITALIC)
    // or
    setTypeface(typeface, Typeface.BOLD_ITALIC)
}
Run Code Online (Sandbox Code Playgroud)


Roh*_*eja 5

由于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);
    }
}
Run Code Online (Sandbox Code Playgroud)