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" /> 
 textview.setTypeface(Typeface.DEFAULT_BOLD); 

setTypeface是属性textStyle。

正如Shankar V所说,为了保留以前设置的字体属性,您可以使用:

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

假设您的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文件中像往常一样创build您的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的手机和平板电脑,没有任何问题。

记住……只有当文本的样式真的取决于你的Java逻辑的一个条件,或者你正在用代码构buildUI的时候,这个function才是有用的,如果没有的话,最好不要做:

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

你可以随心所欲!

希望能帮助到你!

searchsetTextAppearancesetTextTypeface 。 有关于stackoverflow类似的问题: 如何在运行时更改TextView的样式

下面是一些实现这个任务的方法:

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" 

这个问题在很多地方以很多不同的方式提出。 我原本是在这里回答的,但是我觉得它在这个主题中也是相关的(因为当我正在寻找答案的时候,我在这里结束了)。

这个问题没有一个解决scheme,但是这对我的用例起作用。 问题是,'View(context,attrs,defStyle)'构造函数没有引用实际的样式,它需要一个属性。 所以,我们会:

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

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

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

在res / values / styles.xml中,我将创build我想在我的自定义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); 

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

按照说明。

我现有的样式声明:

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

问候。

看看这个。

目前不支持此function。

如何在Android中以编程方式设置样式属性?