如何改变TextView中的字体家族在整个应用程序?

问题描述 投票:1回答:1

我想改变font-familytextview在整个应用程序

目前我使用下面的代码

我已经添加fontres/font文件夹内(Click here to check res/font directory

这里是我的font.xml文件

字体/ 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_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>

这里是我的XML布局

<?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>

这里是我的输出,当我使用qazxsw POI它的正常工作按我的要求

android:fontFamily

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

这里是我的MainActivity的代码

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

现在,当我改变的TextView的字样编程用java我得到下面的结果

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) { } }

OUTPUT using font/font_two" from JAVA

目前我使用上面的代码中面临跌破发行

  • 当我设置OUTPUT using font/font_two" from JAVA使用font-family我得到两个字体效果正常,斜体,但是当我设置XML使用font-family斜体字体不正确设置(可以同时检查的结果,我已经添加了截图)
  • 如何设置java使用font-family
  • 有没有什么办法,为整个应用程序设置
  • 作为java使用font-family怎样才能得到XML的相同的行为

任何机构可以帮助我在此我想改变整个应用程序的java

这里是我所参观过,但没有帮助我解决我的问题的一些其他职位

如需更多信息,请不要让我知道。提前致谢。你的努力将不胜感激。

java android android-layout fonts textview
1个回答
0
投票

有一个库调用书法Changing all typefaces of an android application

或者在构造这样创建一个自定义的TextView,并设置字体就应该更换所有的TextView你个XML来CustomFontTextView

https://github.com/chrisjenx/Calligraphy

Typeface.create()

这不是安全的方法来获得一个字体。

看看这个答案: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)); } }

© www.soinside.com 2019 - 2024. All rights reserved.