Android -设置TextView TextStyle编程?

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

需要明确的是,我不是在谈论视图/小部件样式!我说的是以下几点:

<TextView
android:id="@+id/my_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textStyle="bold" />
300175 次浏览
textview.setTypeface(Typeface.DEFAULT_BOLD);

setTypeface是属性文本样式。

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

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

搜索setTextAppearancesetTextTypeface。在stackoverflow上也有类似的问题:如何改变一个TextView's风格在运行时

正如前面提到的在这里,这个特性目前不支持。

假设你在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 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" />

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

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

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

textViewTitle.setTextAppearance(R.style.RedHUGEText);

使用androidX TextViewCompat来支持所有的API级别

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" />

你可以随心所欲!

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

跟着解释走。

我现有的样式声明:

<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));

的问候。

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

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"

这个问题在很多地方以不同的方式被问到。我最初在这里回答了这个问题,但我觉得它在这个线程中也是相关的(因为当我在寻找答案时,我结束了在这里)。

这个问题没有单一的解决方案,但这对我的用例是有效的。问题是,'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,但它并不要求视图的名称与样式或属性或任何东西匹配。此外,这种技术应该适用于任何自定义视图,而不仅仅是textview。

这对我很有效

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

textview.setTypeface(Typeface.DEFAULT_BOLD);

Kotlin Version

保留当前字体和文本样式:

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

你可以试试这个

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

因为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);
}
}

这对我有用

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

相反了

TextView.setTypeface(Typeface.DEFAULT_BOLD);