Yanıtlar:
textView.setTypeface(null, Typeface.BOLD_ITALIC);
textView.setTypeface(null, Typeface.BOLD);
textView.setTypeface(null, Typeface.ITALIC);
textView.setTypeface(null, Typeface.NORMAL);
Önceki yazı tipini korumak için
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC)
textView.setTypeface(textView.getTypeface(), Typeface.NORMAL);, kalın veya italik stili a TextView. Bunun için kullanmanız gerekecek textView.setTypeface(null, Typeface.NORMAL);.
textView.setTypeface(Typeface.create(textView.getTypeface(), Typeface.NORMAL), Typeface.NORMAL);
TextViewKalın veya italik olarak ayarlamak için bunu deneyin
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
tv.setTypeface(Typeface.create(tv.getTypeface(), Typeface.NORMAL));
tv.setTypeface(null, Typeface.BOLD);bunu yapmayacak mı (mevcut bir yazı tipi stilini temizle)?
Programlı olarak aşağıdakileri kullanarak yapabilirsiniz 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
Doğrudan XML dosyasında aşağıdaki <TextView />gibi ayarlayabilirsiniz :
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
with in Java and without using XMLBu arada başkalarına da yardımcı olacaktır.
İki seçeneğiniz var:
Seçenek 1 (yalnızca kalın, italik ve altı çizili olarak çalışır):
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));
Seçenek 2:
Spannable kullanın ; daha karmaşıktır, ancak metin niteliklerini dinamik olarak değiştirebilirsiniz (yalnızca kalın / italik değil, renkler de).
typeFacemetnin tamamı için tek bir stil ayarlayabilirsiniz.
setTypeface()Yöntemi kullanarak programlı olarak yapabilirsiniz :
Varsayılan Yazı Tipi için kod aşağıdadır
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
ve özel Yazı Tipi ayarlamak istiyorsanız :
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
Doğrudan XML dosyasında şu şekilde ayarlayabilirsiniz <TextView />:
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
Veya fav yazı tipinizi (varlıklardan) ayarlayabilirsiniz. daha fazla bilgi için bağlantıya bakınız
TextView text = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);
şimdi textviewözellikleri ayarlayın ..
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
Olurdu
yourTextView.setTypeface(null,Typeface.DEFAULT_BOLD);
ve italik yerine birlikte olmak gerekir Typeface.DEFAULT_BOLDile Typeface.DEFAULT_ITALC.
Nasıl çalıştığını bana bildirin.
Öğesinin textView.setTypeface(Typeface tf, int style);style özelliğini ayarlamak için kullanın TextView. Daha fazla bilgi için geliştirici belgelerine bakın .
Bunu dene:
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
Bunu dene:
TextView textview = (TextView)findViewById(R.id.textview_idname);
textview.setTypeface(null,Typeface.BOLD);
Bunu yapmanın standart yolu özel stilleri kullanmaktır. eski
Aşağıdakileri styles.xmlekleyin.
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyApp.TextAppearance.LoginText">
<item name="android:textStyle">bold|italic</item>
</style>
Bu stili TextViewaşağıdaki gibi uygulayın .
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/MyApp.TextAppearance.LoginText" />
Bunu yapmanın bir yolu:
myTextView.setTypeface(null, Typeface.ITALIC);
myTextView.setTypeface(null, Typeface.BOLD_ITALIC);
myTextView.setTypeface(null, Typeface.BOLD);
myTextView.setTypeface(null, Typeface.NORMAL);
Önceki yazı tipini korumak ve daha önce uygulanmış olanı kaybetmek istemiyorsanız başka bir seçenek daha sonra:
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);
Bu şekilde deneyebilirsiniz:
<string name="title"><u><b><i>Your Text</i></b></u></string>
Stil seçim ölçütlerine göre yapmanın en kolay yolu:
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));
Özel bir yazı tipi kullanmak istediğim için sadece birkaç cevabın birleşimi benim için çalışıyor. Açıkçası benim layout.xmlgibi ayarları android:textStlyle="italic"AOS tarafından göz ardı edildi. Sonunda aşağıdaki gibi yapmak zorunda kaldım: strings.xmlhedef dize olarak ilan edildi:
<string name="txt_sign"><i>The information blah blah ...</i></string>
sonra ek olarak kod:
TextView textSign = (TextView) findViewById(R.id.txt_sign);
FontHelper.setSomeCustomFont(textSign);
textSign.setTypeface(textSign.getTypeface(), Typeface.ITALIC);
SpannableSeçeneği denemedim (ki çalışmak zorundayız varsayıyorum) ama
textSign.setText(Html.fromHtml(getString(R.string.txt_sign)))
hiçbir etkisi olmadı. Ayrıca ben kaldırırsanız italic taggelen strings.xmlbırakarak setTypeface()o da hiçbir etkisi yoktur tamamen yalnız. Zor Android ...
Ve burada açıklandığı gibi, Android Developers String Resources Eğer tarz metin kaynağınızda parametreleri kullanmanız gerekiyorsa, açılış parantezlerinden kaçmanız gerekir
<resources>
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
</resources>
ve çağrı biçimiHtml (dize)
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
CharSequence styledText = Html.fromHtml(text);
AndroidX ile basitleştirilmiş etiketler kullanırken HtmlCompat.fromHtml () kullanmayı düşünün
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(HtmlCompat.fromHtml(s, FROM_HTML_MODE_LEGACY));
En iyi yol onu tanımlamaktır 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>
Ve güncelleyin TextView
<TextView
android: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" />
1) TypeFace ile ayarlayabilirsiniz. 2) doğrudan kullanabilirsiniz değerleriniz klasöründe (strings.xml olarak) 3) = "Bu benim kalın yazı Dize myNewString olduğunu olabilir benim italik dize Bu edilir Bu benim altı çizili dizedir
Aşağıda verilen örneği kullanarak farklı yazı tipini ayarlayabilirsiniz -
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
Veya farklı bir yazı tipi ve yazı tipi ayarlamak istiyorsanız. Varlığa veya ham klasöre ekleyin ve ardından şu şekilde kullanın
Typeface face= Typeface.createFromAsset(getAssets(), "font/font.ttf");
tv1.setTypeface(face);
Typeface face1= Typeface.createFromAsset(getAssets(), "font/font1.ttf");
tv2.setTypeface(face1);