TextView matnining formatlash

|
TextView matnining formatlash
|
bet | 3/4 | Sana | 27.05.2024 | Hajmi | 227,02 Kb. | | #255047 |
Bog'liq Textviewda matnni tahrirlashTextView matnining formatlash
Android Studio-da TextView matnining formatlashi uchun bir nechta usullar mavjud, masalan:
1. HTML Ishlatish: TextView matnini HTML formatida yozib, `Html.fromHtml()` funktsiyasi orqali ko'rsatish. Bu usul orqali siz matnga farqli shriftlar, ranglar, yorliqlar, va boshqalar kabi formatlashni qo'shishingiz mumkin.
“java”:
TextView textView = findViewById(R.id.textView);
String htmlText = "Bu yorliq matn italic underline";
textView.setText(Html.fromHtml(htmlText));
2. SpannableStringBuilder Ishlatish: SpannableStringBuilder obyektidan foydalanib, matnga turli xil formatlar qo'shish. Masalan, siz matnning bir qismini bold qilish, boshqasini italic qilish, yoki ularga turli xil ranglar berish uchun foydalanishingiz mumkin.
“java”:
TextView textView = findViewById(R.id.textView);
SpannableStringBuilder builder = new SpannableStringBuilder();
// Matn qismini qo'shish
builder.append("Bu matn ");
// Bold format
builder.setSpan(new StyleSpan(Typeface.BOLD), builder.length() - "Bu matn ".length(), builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Italic format
builder.append("italic ", new ItalicSpan(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Underline format
builder.append("underline", new UnderlineSpan(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(builder);
3. Formatlanayotgan Matnni XML Faylga Jo'natish: Agar formatlanayotgan matnni ko'p marta ishlatishingiz kerak bo'lsa, uni string-ressurslar faylida saqlab quyidagicha chaqirish mumkin:
“xml”
Bu yorliq matn italic underline
Keyin, XML faylda TextViewga o'rnating:
“xml”
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/formatted_text" />
Android Studio'da TextView matnining formatlashga quyidagi usullardan foydalanish mumkin:
XML atributlaridan foydalanish:
android:textSize="18sp" - matnning o'lchami
android:textColor="#FF0000" - matnning rangi
android:text - matnning uslubi (qalin, yotiq)
android:gravity="center" - matnni markazlash
android:lineSpacingExtra="4dp" - qatorlar orasidagi masofa
Java/Kotlin kodidan foydalanish:
myTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f) - matnning o'lchami
myTextView.setTextColor(Color.RED) - matnning rangi
myTextView.setTypeface(null, Typeface.BOLD_ITALIC) - matnning uslubi
myTextView.gravity = Gravity.CENTER - matnni markazlash
myTextView.lineSpacingExtra = 4.dpToPixel() - qatorlar orasidagi masofa
Html teglari yordamida:
Spannable API yordamida:
val spannableText = SpannableString("Matn") - Spannable obyektini yaratish
spannableText.setSpan(ForegroundColorSpan(Color.RED), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) - matnning bir qismini rangini o'zgartirish
myTextView.text = spannableText - Spannable matnni TextView-ga o'rnatish
Drawable resurslaridan foydalanish:
myTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_start, 0, R.drawable.ic_end, 0) - matn yoniga rasmlarni qo'shish
Shriflar:
myTextView.typeface = ResourcesCompat.getFont(context, R.font.my_custom_font) - maxsus shriftdan foydalanish
|
| |