Java Android-以编程方式设置TextView TextStyle?
有没有办法通过编程方式设置Java Android-以编程方式设置TextView TextStyle?,java,android,textview,Java,Android,Textview,有没有办法通过编程方式设置TextView的textStyle属性?似乎没有setTextStyle()方法 要清楚,我不是在谈论视图/小部件样式!我所说的是: <TextView android:id="@+id/my_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World" android:textStyle="bo
TextView
的textStyle
属性?似乎没有setTextStyle()
方法
要清楚,我不是在谈论视图/小部件样式!我所说的是:
<TextView
android:id="@+id/my_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textStyle="bold" />
setTypeface是textStyle的属性
正如Shankar V所添加的,要保留以前设置的字体属性,可以使用:
textview.setTypeface(textview.getTypeface(), Typeface.BOLD);
搜索
settext外观
或也搜索settext字体
。stackoverflow上也有类似的问题:如前所述,此功能当前不受支持。假设您的值/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>
这对我有用!它应用了颜色、大小、重力等。我在Android API级别为8到17的手机和平板电脑上使用过它,没有任何问题。请注意,从Android 23开始,该方法已被弃用。上下文参数已被删除,因此最后一行需要是:
textViewTitle.setTextAppearance(R.style.RedHUGEText);
要支持所有API级别,请使用androidXTextViewCompat
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>
关于。实现此任务的多种方法如下:-
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));
tv.setTypeface(null, Typeface.BOLD);
tv.setTypeface(null, Typeface.ITALIC);
tv.setTypeface(null, Typeface.BOLD_ITALIC);
tv.setTypeface(null, Typeface.NORMAL);
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);
<?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);
3.
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));
tv.setTypeface(null, Typeface.BOLD);
tv.setTypeface(null, Typeface.ITALIC);
tv.setTypeface(null, Typeface.BOLD_ITALIC);
tv.setTypeface(null, Typeface.NORMAL);
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);
<?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);
4.
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));
tv.setTypeface(null, Typeface.BOLD);
tv.setTypeface(null, Typeface.ITALIC);
tv.setTypeface(null, Typeface.BOLD_ITALIC);
tv.setTypeface(null, Typeface.NORMAL);
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);
<?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);
这个问题在很多地方都以不同的方式被问到。我最初在这里回答了这个问题,但我觉得它在这个帖子中也是相关的(因为我在寻找答案的时候结束了)
这个问题没有一条线的解决方案,但这对我的用例是有效的。问题是,“View(context,attrs,defStyle)”构造函数没有引用实际的样式,它需要一个属性。因此,我们会:
定义属性
创建要使用的样式
在主题上为该属性应用样式
使用该属性创建视图的新实例
在“res/values/attrs.xml”中,定义一个新属性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="customTextViewStyle" format="reference"/>
...
</resources>
值得注意的是,我在不同的变体和不同的地方反复使用了customTextView,但并不要求视图的名称与样式、属性或任何内容匹配。此外,这种技术应该适用于任何自定义视图,而不仅仅是文本视图。这对我来说很有效
textview.setTypeface(textview.getTypeface(), Typeface.BOLD);
或
•Kotlin版本
要保留文字样式之外的当前字体,请执行以下操作:
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(textViewGoesher,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);
}
}
你可以参考下面的链接,并锁定前1个答案,请检查我的答案。希望这将对您有所帮助这将只设置可在
标记中定义的属性子集。它不适用于常见的方法(尺寸、颜色、样式),但不适用于其他方法(包括填充、背景、重力等),似乎需要最低API 23?有两种方法,一种用于API<23,另一种用于API 23+。它们看起来完全相同,只是其中一个应该是textview.setTypeface(textview.getTypeface(),Typeface.DEFAULT\u BOLD)
@IlyaEremin要保留以前设置的字体,您需要这样使用它。根据@SankarV注释编辑了我的答案。我认为您应该取消默认设置:holder.title.setTypeface(holder.title.getTypeface(),Typeface.BOLD);实际上textview.getTypeface().getStyle()
是android:textStyle
setTextAppearance不推荐的api级别23 api 23+有一个新方法,它不带上下文参数。setTextAppearance()是我想要应用XML中定义的自定义样式的答案。谢谢。在我看来,这是该线程的最佳答案。许多用于以编程方式调用代码的选项
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);
}
}