java - Android - 以编程方式设置TextView TextStyle?
有没有办法以编程方式设置TextView的textStyle属性? 似乎没有setTextStyle()方法。
要清楚,我不是在谈论View / Widget样式! 我在谈论以下内容:
android:id="@+id/my_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textStyle="bold" />
9个解决方案
315 votes
textview.setTypeface(Typeface.DEFAULT_BOLD);
setTypeface是Attribute textStyle。
正如Shankar V补充的那样,为了保留以前设置的字体属性,您可以使用:
textview.setTypeface(textview.getTypeface(), Typeface.BOLD);
Raz answered 2019-03-22T21:49:09Z
100 votes
假设您在values / styles.xml上有一个名为RedHUGEText的样式:
@dimen/text_size_huge
@color/red
bold
只需像往常一样在XML layout / your_layout.xml文件中创建TextView,让我们说:
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的手机和平板电脑上使用它,没有任何问题。
请记住......只有当文本的样式真正依赖于Java逻辑上的条件或者您正在使用代码“动态”构建UI时,这才有用...如果不是,那么最好只是 做:
android:layout_width="fill_parent"
android:layout_height="wrap_content
android:text="FOO"
style="@style/RedHUGEText" />
你可以随时随地拥有它!
希望能帮助到你!
Oscar Salguero answered 2019-03-22T21:50:09Z
58 votes
搜索setTextAppearance或setTextTypeface.在stackoverflow上有类似的问题:如何在运行时更改TextView的样式
peter.bartos answered 2019-03-22T21:50:34Z
43 votes
实现这一任务的方法有很多,如下:
1。
String text_view_str = "Bolded text, italic text, even underlined!";
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。
bold|italic
#FFFFFF
normal
#C0C0C0
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"
duggu answered 2019-03-22T21:51:12Z
5 votes
很多地方都会以很多不同的方式提出这个问题。 我最初在这里回答它,但我觉得它在这个帖子中也是相关的(因为我在搜索答案时最终到了这里)。
这个问题没有一线解决方案,但这适用于我的用例。 问题是,'View(context,attrs,defStyle)'构造函数不引用实际样式,它需要一个属性。 所以,我们将:
定义属性
创建要使用的样式
在我们的主题上为该属性应用样式
使用该属性创建视图的新实例
在'res / values / attrs.xml'中,定义一个新属性:
...
在res / values / styles.xml中,我将创建我想在自定义TextView上使用的样式
18sp
@color/white
14dp
在'res / values / themes.xml'或'res / values / styles.xml'中,修改应用程序/活动的主题并添加以下样式:
@style/CustomTextView
...
最后,在自定义TextView中,您现在可以使用带有属性的构造函数,它将接收您的样式
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context, null, R.attr.customTextView);
}
}
值得注意的是,我在不同的变体和不同的地方重复使用customTextView,但是视图的名称与样式或属性或任何东西都不匹配。 此外,此技术应该适用于任何自定义视图,而不仅仅是TextViews。
Kevin Grant answered 2019-03-22T21:52:44Z
3 votes
这对我有用
textview.setTypeface(textview.getTypeface(), Typeface.BOLD);
要么
textview.setTypeface(Typeface.DEFAULT_BOLD);
Ray Hunter answered 2019-03-22T21:53:06Z
1 votes
我用两种简单的方法解决了它。
按照说明。
我现有的风格声明:
wrap_content
wrap_content
24sp
@color/Church_Grey
@color/Shadow_Church
3
1
1
我的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));
问候。
andresmafra answered 2019-03-22T21:53:52Z
1 votes
***** Kotlin版本*****
要保留当前字体以及文本样式:
textView.apply {
setTypeface(typeface, Typeface.NORMAL)
// or
setTypeface(typeface, Typeface.BOLD)
// or
setTypeface(typeface, Typeface.ITALIC)
// or
setTypeface(typeface, Typeface.BOLD_ITALIC)
}
aminography answered 2019-03-22T21:54:24Z
0 votes
看看这个。
目前不支持此功能。
如何在Android中以编程方式设置样式属性?
dcanh121 answered 2019-03-22T21:55:01Z