كيفية ضبط نمط الخط على Bold ، مائل ومطالبة في Android TextView؟

StackOverflow https://stackoverflow.com/questions/4623508

  •  30-09-2019
  •  | 
  •  

سؤال

أريد أن أجعل ملف TextViewمحتوى جريء ، مائل ومطالبة. جربت الرمز التالي وهو يعمل ، لكن لا تؤكد.

<Textview android:textStyle="bold|italic" ..

كيف فعلتها؟ أي أفكار سريعة؟

هل كانت مفيدة؟

المحلول

لا أعرف عن السطحية ، ولكن بالنسبة للجرأة والمائلة هناك "bolditalic". لا يوجد ذكر للتسديد هنا: http://developer.android.com/reference/android/widget/textview.html#attr_android:TextStyle

ضع في اعتبارك ذلك لاستخدام المذكورة bolditalic تحتاج إلى ذلك ، وأقتبس من تلك الصفحة

يجب أن تكون واحدة أو أكثر (مفصولة بـ '|') من القيم الثابتة التالية.

لذلك كنت تستخدم bold|italic

يمكنك التحقق من هذا السؤال للتسديد: هل يمكنني التأكيد على النص في تصميم Android؟

نصائح أخرى

هذا يجب أن يجعل TextView الخاص بك بالخط العريض, تحتها خط و مائل في نفس الوقت.

سلاسل. xml

<resources>
    <string name="register"><u><b><i>Copyright</i></b></u></string>
</resources>

لتعيين هذه السلسلة على TextView الخاص بك ، قم بذلك في main.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/register" />

أو في جافا,

TextView textView = new TextView(this);
textView.setText(R.string.register);

في بعض الأحيان ، لن يكون النهج أعلاه مفيدًا عندما تضطر إلى استخدام النص الديناميكي. في هذه الحالة Spannablestring يدخل العمل.

String tempString="Copyright";
TextView text=(TextView)findViewById(R.id.text);
SpannableString spanString = new SpannableString(tempString);
spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, spanString.length(), 0);
text.setText(spanString);

انتاج

enter image description here

أو مثل هذا في كوتلين:

val tv = findViewById(R.id.textViewOne) as TextView
tv.setTypeface(null, Typeface.BOLD_ITALIC)
// OR
tv.setTypeface(null, Typeface.BOLD or Typeface.ITALIC)
// OR
tv.setTypeface(null, Typeface.BOLD)
// OR
tv.setTypeface(null, Typeface.ITALIC)
// AND
tv.paintFlags = tv.paintFlags or Paint.UNDERLINE_TEXT_FLAG

أو في جافا:

TextView tv = (TextView)findViewById(R.id.textViewOne);
tv.setTypeface(null, Typeface.BOLD_ITALIC);
// OR
tv.setTypeface(null, Typeface.BOLD|Typeface.ITALIC);
// OR
tv.setTypeface(null, Typeface.BOLD);
// OR
tv.setTypeface(null, Typeface.ITALIC);
// AND
tv.setPaintFlags(tv.getPaintFlags()|Paint.UNDERLINE_TEXT_FLAG);

تبقيه بسيطًا وفي سطر واحد :)

بالنسبة إلى BOLD و ITINIC ، فإن كل ما تفعله صحيح للاستخدام السطحي في الكود التالي

helloandroid.java

 package com.example.helloandroid;

 import android.app.Activity;
 import android.os.Bundle;
 import android.text.SpannableString;
 import android.text.style.UnderlineSpan;
import android.widget.TextView;

public class HelloAndroid extends Activity {
TextView textview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textview = (TextView)findViewById(R.id.textview);
    SpannableString content = new SpannableString(getText(R.string.hello));
    content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
    textview.setText(content);
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"
android:textStyle="bold|italic"/>

String.xml

<?xml version="1.0" encoding="utf-8"?>
 <resources>
  <string name="hello">Hello World, HelloAndroid!</string>
  <string name="app_name">Hello, Android</string>
</resources>

هذه طريقة سهلة لإضافة تسطير ، مع الحفاظ على إعدادات أخرى:

textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

برمجي:

يمكنك القيام برمجي باستخدام طريقة 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:

يمكنك ضبط مباشرة في ملف XML في مثل:

android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"

بدون اقتباسات تعمل بالنسبة لي:

<item name="android:textStyle">bold|italic</item>

إذا كنت تقرأ هذا النص من ملف أو من الشبكة.

يمكنك تحقيق ذلك عن طريق إضافة علامات HTML إلى نصك كما هو مذكور

This text is <i>italic</i> and <b>bold</b>
and <u>underlined</u> <b><i><u>bolditalicunderlined</u></b></i>

وبعد ذلك يمكنك استخدام لغة البرمجة الفئة التي تعالج السلاسل HTML في نص مصمم على غرار.

// textString is the String after you retrieve it from the file
textView.setText(Html.fromHtml(textString));
    style="?android:attr/listSeparatorTextViewStyle
  • من خلال صنع هذا النمط ، يمكنك تحقيق التأكيد
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top