java - 如何在整个应用程序中更改 Textview 的字体系列?

标签 java android android-layout fonts textview

我想在整个应用程序中更改 textviewfont-family

目前我正在使用下面的代码

我在我的 res/font 文件夹中添加了 font ( Click here to check res/font directory )

这是我的font.xml文件

font/font_one

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <font
        android:font="@font/oswald_regular"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/oswald_regular"
        app:fontStyle="normal"
        app:fontWeight="400" />

    <font
        android:font="@font/oswald_bold"
        android:fontStyle="italic"
        android:fontWeight="400"
        app:font="@font/oswald_bold"
        app:fontStyle="italic"
        app:fontWeight="400" />

</font-family>

@font/font_two

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <font
        android:font="@font/opensans_regular"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/opensans_regular"
        app:fontStyle="normal"
        app:fontWeight="400" />
    <font
        android:font="@font/opensans_bold"
        android:fontStyle="italic"
        android:fontWeight="400"
        app:font="@font/opensans_bold"
        app:fontStyle="italic"
        app:fontWeight="400" />

</font-family>

here is my xml layout

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/sampleTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="?backColor"
            android:fontFamily="@font/font_two"
            android:padding="20dp"
            android:text="@string/app_name"
            android:textColor="?textColor"
            android:textStyle="normal" />

        <TextView
            android:id="@+id/sampleTextView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="?backColor"
            android:fontFamily="@font/font_one"
            android:padding="20dp"
            android:text="@string/app_name"
            android:textColor="?textColor"
            android:textStyle="italic" />
    </LinearLayout>


    <Spinner
        android:id="@+id/fontSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" />

    <Spinner
        android:id="@+id/themeSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" />


    <Button
        android:id="@+id/btnChaneFont"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/test"
        android:text="Change Font"
        android:textColor="?textColor" />


    <Button
        android:id="@+id/btnChaneTheme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/test"
        android:text="Change Theme"
        android:textColor="?textColor" />


</LinearLayout>

here is my output when i'm using android:fontFamily its woking fine as per my requirement

OUTPUT using android:fontFamily="@font/font_one" from XML

OUTPUT using android:fontFamily="@font/font_two" from XML

here is the code of my MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener {

    Spinner fontSpinner, themeSpinner;

    Button btnChaneFont, btnChaneTheme;

    TextView sampleTextView, sampleTextView2;

    int theme = R.style.AppTheme;

    String[] fontsArray = new String[]{"Oswald Fonts", "Open Sans Fonts", "Raleway Fonts"};
    String[] themeArray = new String[]{"Theme 1", "Theme 2", "Theme 3"};

    Typeface typeface = null;

    PrefManager prefManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        new ThemeColors(this);

        prefManager = new PrefManager(this);
        getTheme().applyStyle(prefManager.getTheme(), true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {

        fontSpinner = findViewById(R.id.fontSpinner);
        themeSpinner = findViewById(R.id.themeSpinner);

        btnChaneFont = findViewById(R.id.btnChaneFont);
        btnChaneTheme = findViewById(R.id.btnChaneTheme);

        btnChaneFont.setOnClickListener(this);
        btnChaneTheme.setOnClickListener(this);

        fontSpinner.setOnItemSelectedListener(this);
        themeSpinner.setOnItemSelectedListener(this);

        sampleTextView = findViewById(R.id.sampleTextView);
        sampleTextView2 = findViewById(R.id.sampleTextView2);

        ArrayAdapter<String> fontAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, fontsArray);
        fontSpinner.setAdapter(fontAdapter);

        ArrayAdapter<String> themeAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, themeArray);
        themeSpinner.setAdapter(themeAdapter);

    }


    @Override
    public void onClick(View view) {

        if (view == btnChaneTheme) {
            recreate();
        }

        if (view == btnChaneFont) {

            sampleTextView.setTypeface(typeface);
            sampleTextView2.setTypeface(typeface);
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {

        switch (parent.getId()) {
            case R.id.fontSpinner:
                switch (i) {
                    case 0:
                        typeface = ResourcesCompat.getFont(MainActivity.this, R.font.font_one);
                        break;
                    case 1:
                        typeface = ResourcesCompat.getFont(MainActivity.this, R.font.font_two);
                        break;
                    case 2:
                        typeface = ResourcesCompat.getFont(MainActivity.this, R.font.font_three);
                        break;
                }
                break;

            case R.id.themeSpinner:
                switch (i) {
                    case 0:
                        prefManager.setTheme(R.style.AppTheme);
                        break;
                    case 1:
                        prefManager.setTheme(R.style.AppTheme2);
                        break;
                    case 2:
                        prefManager.setTheme(R.style.AppTheme3);
                        break;
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}

现在,当我使用 java 以编程方式更改 TextView 的字体时,结果低于

OUTPUT using font/font_two" from JAVA

OUTPUT using font/font_two" from JAVA

Currently i'm facing below issue using above code

  • 当我使用 XML 设置 font-family 时,我得到了两种字体效果 normalitalic 但是当我使用 java 设置 font-family 时,italic 字体设置不正确(你可以检查我的两个结果已添加屏幕截图)
  • 如何使用java设置font-family
  • 有没有办法为整个应用程序设置
  • 如何使用 java 获得与 XML 相同的 font-family 行为

任何人都可以帮助我吗我想改变整个应用程序的font-family

here are some other post that i have visited but didn't help me to solve my problem

如果需要更多信息,请告诉我。提前致谢。我们将不胜感激。

最佳答案

有一个书法图书馆 https://github.com/chrisjenx/Calligraphy

或者创建自定义 TextView 并在构造函数中设置字体 通过这种方式,您应该将 xml 中的所有 TextView 替换为 CustomFontTextView

import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

public class CustomFontTextView extends AppCompatTextView {

    public CustomFontTextView(Context context) {
        super(context);
        setCustomFont();
    }

    public CustomFontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont();
    }

    public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont();
    }

    private void setCustomFont() {
        setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
    }

}

Typeface.create()

获取字体是不安全的方法。

看看这个答案: https://stackoverflow.com/a/16902532/10995239

关于java - 如何在整个应用程序中更改 Textview 的字体系列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54548205/

相关文章:

java - 解析字符串以在 Java 中构造树

java - 如何在 Controller 外部使用 JPA 存储库更新行值?

java - 如何在 Java 中获取对象类?

android - Android 布局问题

java - ValueAnimator 翻译动画

javascript - 移动设备的屏幕宽度(android和ipod)

android - OnPause() 之后会发生什么,仍然可以调用监听器吗?

java - RxJava在没有响应体时捕获网络响应代码

android - SlidingMenu 滑动但不移动后 View

android - 如何在 EditText 中放置按钮