Jus*_*tMe 806 android styles textview
如何TextView
在Java中设置样式(粗体斜体)而不使用XML布局?
换句话说,我需要android:textStyle
用Java 编写.
Tan*_*dal 1810
textView.setTypeface(null, Typeface.BOLD_ITALIC);
textView.setTypeface(null, Typeface.BOLD);
textView.setTypeface(null, Typeface.ITALIC);
textView.setTypeface(null, Typeface.NORMAL);
Run Code Online (Sandbox Code Playgroud)
保留以前的字体
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC)
Run Code Online (Sandbox Code Playgroud)
Nir*_*tel 263
尝试将此设置TextView
为粗体或斜体
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
Run Code Online (Sandbox Code Playgroud)
Pra*_*ani 134
您可以使用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
Run Code Online (Sandbox Code Playgroud)
您可以直接在XML文件中设置<TextView />
:
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
Run Code Online (Sandbox Code Playgroud)
Gab*_*gut 91
您有两种选择:
选项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));
Run Code Online (Sandbox Code Playgroud)
选项2:
使用Spannable ; 它更复杂,但您可以动态修改文本属性(不仅是粗体/斜体,还有颜色).
aks*_*hay 38
您可以使用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
Run Code Online (Sandbox Code Playgroud)
如果要设置自定义字体:
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
Run Code Online (Sandbox Code Playgroud)
您可以直接在XML文件中设置<TextView />
如下:
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
Run Code Online (Sandbox Code Playgroud)
或者您可以设置您的fav字体(来自资产).有关更多信息,请参阅链接
Rav*_*avi 15
您可以使用下面给出的示例设置不同的字体 -
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
Run Code Online (Sandbox Code Playgroud)
或者如果您想设置不同的字体及其字样。将其添加到 asset 或 raw 文件夹中,然后像这样使用它
Typeface face= Typeface.createFromAsset(getAssets(), "font/font.ttf");
tv1.setTypeface(face);
Typeface face1= Typeface.createFromAsset(getAssets(), "font/font1.ttf");
tv2.setTypeface(face1);
Run Code Online (Sandbox Code Playgroud)
Aka*_*kar 12
TextView text = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);
Run Code Online (Sandbox Code Playgroud)
现在设置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
Run Code Online (Sandbox Code Playgroud)
Nik*_*hil 10
尝试TextView
通过java代码设置你的风格
txt1.setTypeface(null,Typeface.BOLD_ITALIC);
Run Code Online (Sandbox Code Playgroud)
小智 9
TextView text = (TextView)findViewById(R.layout.textName);
text.setTypeface(null,Typeface.BOLD);
Run Code Online (Sandbox Code Playgroud)
这将是
yourTextView.setTypeface(null,Typeface.DEFAULT_BOLD);
Run Code Online (Sandbox Code Playgroud)
斜体应该可以替换Typeface.DEFAULT_BOLD
为Typeface.DEFAULT_ITALC
.
让我知道它是如何工作的.
textView.setTypeface(null, Typeface.BOLD_ITALIC);
textView.setTypeface(null, Typeface.BOLD);
textView.setTypeface(null, Typeface.ITALIC);
textView.setTypeface(null, Typeface.NORMAL);
Run Code Online (Sandbox Code Playgroud)
保留之前的字体
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC)
Run Code Online (Sandbox Code Playgroud)
小智 7
尝试这个:
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
Run Code Online (Sandbox Code Playgroud)
小智 5
试试这个:
TextView textview = (TextView)findViewById(R.id.textview_idname);
textview.setTypeface(null,Typeface.BOLD);
Run Code Online (Sandbox Code Playgroud)