programing

Android에서 텍스트를 굵게 변경하려면 어떻게 해야 합니까?

telebox 2023. 6. 6. 08:12
반응형

Android에서 텍스트를 굵게 변경하려면 어떻게 해야 합니까?

Android에서 텍스트/글꼴 설정을 어떻게 변경합니까?

예를 들어 텍스트를 굵게 표시하려면 어떻게 해야 합니까?

에서 이 작업을 수행하려면layout.xml파일:

android:textStyle

예:

android:textStyle="bold|italic"

프로그래밍 방식은 다음과 같습니다.

setTypeface(Typeface tf)

텍스트를 표시할 서체 및 스타일을 설정합니다.모든 것은 아닙니다.Typeface가족에는 실제로 대담하고 기울임꼴 변형이 있으므로 사용해야 할 수 있습니다.setTypeface(Typeface, int)당신이 실제로 원하는 모습을 얻기 위해서.

여기 해결책이 있습니다.

TextView questionValue = (TextView) findViewById(R.layout.TextView01);
questionValue.setTypeface(null, Typeface.BOLD);

간단히 다음을 수행할 수 있습니다.

속성 설정 위치XML

  android:textStyle="bold"

프로그래밍 방식은 다음과 같습니다.

TextView Tv = (TextView) findViewById(R.id.TextView);

Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);

Tv.setTypeface(boldTypeface);

이것이 당신에게 도움이 되길 바랍니다.

XML에서

android:textStyle="bold" //only bold
android:textStyle="italic" //only italic
android:textStyle="bold|italic" //bold & italic

특정 글꼴만 사용할 수 있습니다.sans,serif&monospacexml을 통해 자바 코드는 사용자 정의 글꼴을 사용할 수 있습니다.

android:typeface="monospace" // or sans or serif

프로그래밍 방식(자바 코드)

TextView textView = (TextView) findViewById(R.id.TextView1);

textView.setTypeface(Typeface.SANS_SERIF); //only font style
textView.setTypeface(null,Typeface.BOLD); //only text style(only bold)
textView.setTypeface(null,Typeface.BOLD_ITALIC); //only text style(bold & italic)
textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD); 
                                         //font style & text style(only bold)
textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD_ITALIC);
                                         //font style & text style(bold & italic)

XML에서 textStyle아래와 같이 굵게 설정할 수 있습니다.

<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Bold text"
   android:textStyle="bold"/>

아래와 같이 프로그래밍 방식으로 TextView를 굵게 설정할 수 있습니다.

textview.setTypeface(Typeface.DEFAULT_BOLD);

사용자 지정 글꼴을 사용하지만 글꼴에 굵은 글꼴이 없는 경우 다음을 사용할 수 있습니다.

myTextView.setText(Html.fromHtml("<b>" + myText + "</b>");

속성 설정

android:textStyle="bold"

그것은 매우 쉽습니다.

setTypeface(Typeface.DEFAULT_BOLD);

그림을 그리는 경우 다음과 같이 수행합니다.

TextPaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);

이상적인 환경에서는 레이아웃 XML 정의에서 텍스트 스타일 속성을 다음과 같이 설정할 수 있습니다.

<TextView
    android:id="@+id/TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"/>

메소드를 사용하여 코드에서 동일한 결과를 동적으로 얻을 수 있는 간단한 방법이 있습니다.TextView에 대한 글꼴 스타일을 설명하는 Typeface 클래스의 개체와 전달해야 합니다.따라서 위의 XML 정의와 동일한 결과를 얻으려면 다음 작업을 수행합니다.

TextView Tv = (TextView) findViewById(R.id.TextView);
Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
Tv.setTypeface(boldTypeface);

첫 번째 줄은 객체 양식의 미리 정의된 스타일(이 경우 유형)을 작성합니다.굵은 글씨로 표시됩니다. 하지만 사전 정의된 것이 더 많습니다.)서체 인스턴스가 생성되면 TextView에서 설정할 수 있습니다.이상으로 컨텐츠가 정의된 스타일에 표시됩니다.

당신에게 많은 도움이 되길 바랍니다.자세한 내용은 다음 사이트를 참조하십시오.

http://developer.android.com/reference/android/graphics/Typeface.html

XML을 통해:

 android:textStyle="bold"

Java를 통해:

//Let's say you have a textview 
textview.setTypeface(null, Typeface.BOLD);

values 폴더의 style.xml 파일에 원하는 형식으로 새 스타일 정의

<style name="TextViewStyle" parent="AppBaseTheme">
    <item name="android:textStyle">bold</item>
    <item name="android:typeface">monospace</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">#5EADED</item>

</style>

그런 다음 TextView의 속성으로 다음 코드를 작성하여 이 스타일을 TextView에 적용합니다.

style="@style/TextViewStyle"

가장 좋은 방법은 다음과 같습니다.

TextView tv = findViewById(R.id.textView);
tv.setTypeface(Typeface.DEFAULT_BOLD);

.xml 파일에서, 설정

android:textStyle="bold" 

텍스트 유형을 굵게 설정합니다.

Android TextView를 굵게 만드는 4가지 방법 - 전체 답이 여기 있습니다.

  1. Android:textStyle 특성 사용

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TEXTVIEW 1" android:textStyle="bold" /> 굵고 기울임꼴인 경우 bold|이탤릭체를 사용합니다.

  2. setTypeface() 메서드 사용

    textview2.setTypeface(null, Typeface.BOLD);
    textview2.setText("TEXTVIEW 2");
    
  3. Html() 메서드의 HtmlCompat.fromHtml()은 API 수준 24에서 더 이상 사용되지 않습니다.

     String html="This is <b>TEXTVIEW 3</b>";
     textview3.setText(HtmlCompat.fromHtml(html,Typeface.BOLD));
    

Android Studio의 새로운 시작자라고 가정하면 다음을 사용하여 디자인 XML에서 간단히 수행할 수 있습니다.

android:textStyle="bold"          //to make text bold
android:textStyle="italic"        //to make text italic
android:textStyle="bold|italic"   //to make text bold & italic

글꼴에 사용할 수 있습니다.

클래스 이름 유형을 작성하고 텍스트 보기를 확장합니다.

개인 정적 MapType 면;

public TypefaceTextView(final Context context) {
    this(context, null);
}

public TypefaceTextView(final Context context, final AttributeSet attrs) {
    this(context, attrs, 0);
}

public TypefaceTextView(final Context context, final AttributeSet attrs, final int defStyle) {
    super(context, attrs, defStyle);
    if (mTypefaces == null) {
        mTypefaces = new HashMap<String, Typeface>();
    }

    if (this.isInEditMode()) {
        return;
    }

    final TypedArray array = context.obtainStyledAttributes(attrs, styleable.TypefaceTextView);
    if (array != null) {
        final String typefaceAssetPath = array.getString(
                R.styleable.TypefaceTextView_customTypeface);

        if (typefaceAssetPath != null) {
            Typeface typeface = null;

            if (mTypefaces.containsKey(typefaceAssetPath)) {
                typeface = mTypefaces.get(typefaceAssetPath);
            } else {
                AssetManager assets = context.getAssets();
                typeface = Typeface.createFromAsset(assets, typefaceAssetPath);
                mTypefaces.put(typefaceAssetPath, typeface);
            }

            setTypeface(typeface);
        }
        array.recycle();
    }
}

자산 폴더에 작성된 글꼴 폴더에 글꼴을 붙여넣습니다.

<packagename.TypefaceTextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.5"
        android:gravity="center"
        android:text="TRENDING TURFS"
        android:textColor="#000"
        android:textSize="20sp"
        app:customTypeface="fonts/pompiere.ttf" />**here pompiere.ttf is the font name**

xml의 부모 레이아웃에 선을 배치합니다.

 xmlns:app="http://schemas.android.com/apk/res/com.mediasters.wheresmyturf"
xmlns:custom="http://schemas.android.com/apk/res-auto"

저의 경우 string.xml을 통한 값 전달이 html Tag로 해결되었습니다.

<string name="your_string_tag"> <b> your_text </b></string>

editText.setTypeface(Typeface.createFromAsset(getAssets(), ttfFilePath));
etitText.setTypeface(et.getTypeface(), Typeface.BOLD);

글꼴과 스타일을 모두 굵게 설정합니다.

코틀린에서 우리는 한 줄로 할 수 있습니다.

 TEXT_VIEW_ID.typeface = Typeface.defaultFromStyle(Typeface.BOLD)

당신은 이걸 할 수 있다.

ty.setTypeface(Typeface.createFromAsset(ctx.getAssets(), "fonts/magistral.ttf"), Typeface.BOLD);
textView.setPaintFlags(textView.getPaintFlags() | Paint.FAKE_BOLD_TEXT_FLAG)

제거하려면, 사용

textView.setPaintFlags(textView.getPaintFlags() & ~Paint.FAKE_BOLD_TEXT_FLAG)

또는 코틀린어로:

fun TextView.makeBold() {
    this.paintFlags = this.paintFlags or Paint.FAKE_BOLD_TEXT_FLAG
}

fun TextView.removeBold() {
    this.paintFlags = this.paintFlags and (Paint.FAKE_BOLD_TEXT_FLAG.inv())
}

언급URL : https://stackoverflow.com/questions/4792260/how-do-you-change-text-to-bold-in-android

반응형