Java 使用自定义字体(编程方式)在文本视图中设置粗体和斜体样式

Java 使用自定义字体(编程方式)在文本视图中设置粗体和斜体样式,java,android,fonts,Java,Android,Fonts,我有一个文本视图,我希望它是粗体和斜体。我应用我的自定义字体,但只让我做粗体或斜体。如何将这两种样式同时应用于文本视图?您需要一种粗体和斜体的字体,这就是字体的工作方式。不幸的是,Android不能简单地将它们结合起来,因为这并不容易,所以你需要有自己的文件。你需要一种粗体和斜体的字体,这就是字体的工作原理。不幸的是,Android不能简单地将它们结合起来,因为这并不容易,所以您需要有自己的文件。您是否尝试在XML文件中设置: android:textStyle="bold|italic" 在

我有一个文本视图,我希望它是粗体和斜体。我应用我的自定义字体,但只让我做粗体或斜体。如何将这两种样式同时应用于文本视图?

您需要一种粗体和斜体的字体,这就是字体的工作方式。不幸的是,Android不能简单地将它们结合起来,因为这并不容易,所以你需要有自己的文件。

你需要一种粗体和斜体的字体,这就是字体的工作原理。不幸的是,Android不能简单地将它们结合起来,因为这并不容易,所以您需要有自己的文件。

您是否尝试在XML文件中设置:

android:textStyle="bold|italic"
在程序上:

textView.setTypeface(yourTypeFace, Typeface.BOLD_ITALIC);
例如:

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.fontapp.MainActivity">

    <TextView
        android:id="@+id/normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="16sp"
         />
    <TextView
        android:id="@+id/bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="16sp"
        />
    <TextView
        android:id="@+id/italic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="16sp"
    />
    <TextView
        android:id="@+id/boldItalic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="16sp"
        />
</LinearLayout>

您是否尝试在XML文件中设置:

android:textStyle="bold|italic"
在程序上:

textView.setTypeface(yourTypeFace, Typeface.BOLD_ITALIC);
例如:

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.fontapp.MainActivity">

    <TextView
        android:id="@+id/normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="16sp"
         />
    <TextView
        android:id="@+id/bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="16sp"
        />
    <TextView
        android:id="@+id/italic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="16sp"
    />
    <TextView
        android:id="@+id/boldItalic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="16sp"
        />
</LinearLayout>

但是我应用了我的自定义字体(使用ttf文件)(以编程方式)。我想用粗体和斜体显示我的文本视图,我使用我自己的字体。我的意思是,我有包含自定义字体的外部.ttf文件。据我所知,如果你想把你的自定义字体放在其中一个视图中,你必须通过编程(使用字体和加载你的.ttf文件所在的路径并进行设置)。但我只能用粗体或斜体,不能同时使用这两种样式。你看到更新了吗“你的字体”是你的字体。对不起,伙计,这不是答案。谢谢你,巴迪,我添加了一个使用常规ttf字体的工作示例,但是我应用了我的自定义字体(使用ttf文件)(以编程方式)。我想用粗体和斜体显示我的文本视图,我使用我自己的字体。我的意思是,我有包含自定义字体的外部.ttf文件。据我所知,如果你想把你的自定义字体放在其中一个视图中,你必须通过编程(使用字体和加载你的.ttf文件所在的路径并进行设置)。但我只能用粗体或斜体,不能同时使用这两种样式。你看到更新了吗“你的字体”是你的字体。对不起,伙计,这不是答案。谢谢,伙计,我添加了一个使用常规ttf字体的工作示例