如何设置TextView textStyle,例如粗体,斜体
如何在Java中设置TextView
样式(粗体或斜体)而不使用XML布局?
换句话说,我需要用Java编写android:textStyle
。
#1楼
TextView text = (TextView)findViewById(R.layout.textName);
text.setTypeface(null,Typeface.BOLD);
#2楼
以编程方式:
您可以使用setTypeface()
编程方式进行setTypeface()
:
textView.setTypeface(null, Typeface.NORMAL); // for Normal Text
textView.setTypeface(null, Typeface.BOLD); // for Bold only
textView.setTypeface(null, Typeface.ITALIC); // for Italic
textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic
XML:
您可以在<TextView />
中的XML文件中直接设置,例如:
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
#3楼
执行此操作的标准方法是使用自定义样式。 前
在styles.xml
添加以下内容。
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyApp.TextAppearance.LoginText"><item name="android:textStyle">bold|italic</item>
</style>
将此样式应用于TextView
,如下所示。
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"style="@style/MyApp.TextAppearance.LoginText" />
#4楼
由于我想使用自定义字体,因此只有几个答案的结合对我有效。 显然,我的layout.xml
设置(例如android:textStlyle="italic"
)被AOS忽略了。 因此,最后我必须执行以下操作:在strings.xml
,目标字符串声明为:
<string name="txt_sign"><i>The information blah blah ...</i></string>
然后另外在代码中:
TextView textSign = (TextView) findViewById(R.id.txt_sign);
FontHelper.setSomeCustomFont(textSign);
textSign.setTypeface(textSign.getTypeface(), Typeface.ITALIC);
我没有尝试过Spannable
选项(我认为必须工作),但是
textSign.setText(Html.fromHtml(getString(R.string.txt_sign)))
没有效果。 另外,如果我从strings.xml
删除italic tag
,而将setTypeface()
全部保留setTypeface()
,则也没有任何效果。 棘手的Android ...
#5楼
尝试这个:
TextView textview = (TextView)findViewById(R.id.textview_idname);
textview.setTypeface(null,Typeface.BOLD);
#6楼
以编程方式:
您可以使用setTypeface()
方法以编程方式进行操作:
以下是默认字体的代码
textView.setTypeface(null, Typeface.NORMAL); // for Normal Text
textView.setTypeface(null, Typeface.BOLD); // for Bold only
textView.setTypeface(null, Typeface.ITALIC); // for Italic
textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic
以及是否要设置自定义字体 :
textView.setTypeface(textView.getTypeface(), Typeface.NORMAL); // for Normal Text
textView.setTypeface(textView.getTypeface(), Typeface.BOLD); // for Bold only
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC); // for Italic
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC); // for Bold and Italic
XML:
您可以像这样在<TextView />
中的XML文件中直接进行设置:
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
或者,您可以设置收藏字体(从素材资源)。 有关更多信息, 请参见链接
#7楼
如此处所述, Android Developers String Resources如果您需要在样式化的文本资源中使用参数,则必须使用方括号
<resources>
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
</resources>
并调用formatHtml(string)
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
CharSequence styledText = Html.fromHtml(text);
#8楼
最好的方法是在styles.xml
定义它
<style name="common_txt_style_heading" parent="android:style/Widget.TextView"><item name="android:textSize">@dimen/common_txtsize_heading</item><item name="android:textColor">@color/color_black</item><item name="android:textStyle">bold|italic</item>
</style>
并在TextView
更新
<TextViewandroid:id="@+id/txt_userprofile"style="@style/common_txt_style_heading"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_marginTop="@dimen/margin_small"android:text="@string/some_heading" />
#9楼
只是如果您想使文本加粗 。 在文本视图属性中的布局中写入此行
android:textStyle="bold"
#10楼
AppCompatTextView text =(AppCompatTextView)findViewById(R.layout.appCompatTextView1);
text.setTypeface(null,Typeface.BOLD);
使用上述方法以编程方式设置字体。
#11楼
一种方法是:
myTextView.setTypeface(null, Typeface.ITALIC);
myTextView.setTypeface(null, Typeface.BOLD_ITALIC);
myTextView.setTypeface(null, Typeface.BOLD);
myTextView.setTypeface(null, Typeface.NORMAL);
如果您想保留以前的字体并且不想丢失以前应用的另一种选择,请执行以下操作:
myTextView.setTypeface(textView.getTypeface(), Typeface.NORMAL);
myTextView.setTypeface(textView.getTypeface(), Typeface.BOLD);
myTextView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
myTextView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
#12楼
您可以这样尝试:
<string name="title"><u><b><i>Your Text</i></b></u></string>
#13楼
就我而言:
1-设定文字
2-设置字体
holder.title.setText(item.nome);
holder.title.setTypeface(null, Typeface.BOLD);
#14楼
这是对配置了OnePlus Slate™字体的OnePlus 5T唯一适用于我的东西:
textView.setTypeface(Typeface.create(textView.getTypeface(), useBold ? Typeface.BOLD : Typeface.NORMAL));
其他方法会使其在变为BOLD或NORMAL时退回到Roboto。
#15楼
textView.setTypeface(null, Typeface.BOLD_ITALIC);
textView.setTypeface(null, Typeface.BOLD);
textView.setTypeface(null, Typeface.ITALIC);
textView.setTypeface(null, Typeface.NORMAL);
保留以前的字体
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC)
#16楼
尝试这个:
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
#17楼
根据样式选择标准,最简单的方法是:
String pre = "", post = "";if(isBold){pre += "<b>"; post += "</b>";
}
if(isItalic){pre += "<i>"; post += "</i>";
}
if(isUnderline){pre += "<u>"; post += "</u>";
}textView.setText(Html.fromHtml(pre + editText.getText().toString()+ post));
// you can also use it with EidtText
editText.setText(Html.fromHtml(pre + editText.getText().toString()+ post));
#18楼
您有两种选择:
选项1 (仅适用于粗体,斜体和下划线):
String s = "<b>Bolded text</b>, <i>italic text</i>, even <u>underlined</u>!"
TextView tv = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);
tv.setText(Html.fromHtml(s));
选项2:
使用Spannable ; 它比较复杂,但是您可以动态修改文本属性(不仅是粗体/斜体,还可以是颜色)。
#19楼
这将是
yourTextView.setTypeface(null,Typeface.DEFAULT_BOLD);
和斜体应该可以用Typeface.DEFAULT_BOLD
替换Typeface.DEFAULT_ITALC
。
让我知道它是如何工作的。
#20楼
使用textView.setTypeface(Typeface tf, int style);
设置TextView
style属性。 有关更多信息,请参见开发人员文档 。
#21楼
尝试在TextView
上设置为粗体或斜体
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
#22楼
尝试通过Java代码设置您的TextView
样式
txt1.setTypeface(null,Typeface.BOLD_ITALIC);
#23楼
textView.setTypeface(null, Typeface.BOLD_ITALIC);
textView.setTypeface(null, Typeface.BOLD);
textView.setTypeface(null, Typeface.ITALIC);
textView.setTypeface(null, Typeface.NORMAL);
保留以前的字体
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC)
#24楼
TextView text = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);
现在设置textview
属性。
text.setTypeface(null, Typeface.BOLD); //-- for only bold the text
text.setTypeface(null, Typeface.BOLD_ITALIC); //-- for bold & italic the text
text.setTypeface(null, Typeface.ITALIC); // -- for italic the text
如何设置TextView textStyle,例如粗体,斜体相关推荐
- java斜体_设置TextView样式(粗体或斜体)
回答(21) 2 years ago TextView text = (TextView)findViewById(R.layout.textName); text.setTypeface(null, ...
- android字体斜体代码,Android设置Roboto字体用粗体,斜体,常规,…(类似于自定义字体系列)...
我知道在Android应用程序中以编程方式设置自定义字体. 有没有办法为自定义字体(资产)加载字体,Android框架将使用基于粗体,斜体等的正确文件? 例如,现在我正在尝试将Roboto字体设置为某 ...
- html中设置字体字型,html中 如何在font中设置字体样式(粗体斜体等)
html> 字体加粗 用<b>加粗字体:加粗 用<strong>加粗字体:加粗 用CSS font-weight 加粗 normal(400), bold(700) ...
- android中TextView中文字体粗体的方法 (android:textStyle=bold)
android中TextView中文字体粗体的方法 (android:textStyle="bold"来使字体显示为粗体,但是这只对英文有效) TextView在xml中可以定义a ...
- 设置字体同时为粗体、斜体
设置字体样式操作比较简单,如下语句: FontStyle style = FontStyle.Bold; 即可设置字体样式为粗体.当需要设置字体样式同时具有多种风格时,可以采用按位或 ...
- Android 设置TextView字体加粗
今天,简单讲讲Android里如何设置TextView字体加粗. 不废话了,用过多次,还是没记住.直接上代码. 1.布局文件中这样设置即可: XML/HTML代码 android:textStyle= ...
- R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,只设置x轴的标签文本使用粗体字体)
R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,只设置x轴的标签文本使用粗体字体) 目录
- R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,只设置y轴的标签文本使用粗体字体)
R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,只设置y轴的标签文本使用粗体字体) 目录
- R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,使x轴和Y轴的标签文本都使用粗体字体)、注意是轴标签而非轴标题
R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,使x轴和Y轴的标签文本都使用粗体字体).注意是轴标签而非轴标题 目录
最新文章
- java内部类 缺点_Java中的内部类及其优势
- 包邮送书 | 中科院博士推荐的50本高质量Python、数据分析书籍
- kubeadm 线上集群部署(四) k8s node 节点初始化安装
- 多继承有什么坏处,为什么java搞单继承,接口为什么可以摈弃这些坏处
- 华为鲲鹏弹性云服务器KM1_#化鲲为鹏,我有话说# 鲲鹏弹性云服务器配置 Tomcat...
- javaScript执行环境、作用域链与闭包
- spring boot 扩展之AutoConfigurationImportListener
- GOOGLE不让我访问啦?
- 【知识补充】对称加密、非对称加密、数字签名与DDoS攻击
- vmware挂载san存储_细述企业级存储NAS和SAN差异
- 【API进阶之路】逆袭!用关键词抽取API搞定用户需求洞察
- Python机器学习:梯度下降法003线性回归中的梯度下降法
- 使用 Nginx 部署静态页面
- bootstrap 和 jqueryui
- win10+ubuntu双系统引导修复
- 计算机四级等级考试网络工程师知识点-【操作系统原理+计算机网络两科完整】
- CM311-1a linux游戏服务器操作立马拥有我的世界私人服务器哦
- java 获取拼音_Java获取汉字对应的拼音(全拼或首字母)
- 高速PCB基础-电源分配网络
- “信息系统灾难恢复方案设计”大赛
热门文章
- python训练好的图片验证_利用keras加载训练好的.H5文件,并实现预测图片
- 【Android基础】从属性动画看代码设计的艺术
- UIView 的布局与绘制显示相关方法调用时机
- animated bar chart race下载_下载腾讯会议
- thinkphp概述2
- uni-app编译配置
- vs2019装了WDK后,编译其他vc工程,提示无法打开文件msvcprtd.lib
- LeetCode题目:两数之和2
- Android Studio教程10-Intent的详细使用
- DO YOU WANNA BUILD A SNOW MAN ?