两个字体文件

时间:2017-09-30 05:08:15

标签: android

假设我的应用程序资产non_unicode.ttf和unicode.ttf中有两种字体。我想制作两个按钮[unicode和non_unicode]。由此,用户可以更改整个应用程序的字体。当用户单击unicode按钮时,我想在所有字符串资源中显示unicode字体,然后单击我想要的non_unicode按钮 在所有字符串资源中显示non_unicode字体。但是我有一个strings.xml文件,值为mm [mm for myanmar,这是我的语言]文件夹。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

首先在 main/assets/fonts 文件夹中添加 .ttf文件,如下所示:

enter image description here

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button btn1, btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        final ViewGroup mContainer = (ViewGroup) findViewById(
                android.R.id.content).getRootView();
        final Typeface mFont1 = Typeface.createFromAsset(getAssets(),
                "fonts/Oswald-Regular.ttf");
        final Typeface mFont2 = Typeface.createFromAsset(getAssets(),
                "fonts/ufonts.com_malgun-gothic.ttf");

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setWholeAppFont(mContainer, mFont1,true);
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setWholeAppFont(mContainer, mFont2,true);
            }
        });
    }
    public static final void setWholeAppFont(ViewGroup mContainer, Typeface mFont, boolean reflect)
    {
        if (mContainer == null || mFont == null) return;

        final int mCount = mContainer.getChildCount();

        // Loop through all of the children.
        for (int i = 0; i < mCount; ++i)
        {
            final View mChild = mContainer.getChildAt(i);
            if (mChild instanceof TextView)
            {
                // Set the font if it is a TextView.
                ((TextView) mChild).setTypeface(mFont);
            }
            else if (mChild instanceof ViewGroup)
            {
                // Recursively attempt another ViewGroup.
                setWholeAppFont((ViewGroup) mChild, mFont, true);
            }
            else if (reflect)
            {
                try {
                    Method mSetTypeface = mChild.getClass().getMethod("setTypeface", Typeface.class);
                    mSetTypeface.invoke(mChild, mFont);
                } catch (Exception e) {
                    /* Do something... */
                    Log.e("TAG", "setAppFont: " + e.getLocalizedMessage());
                }
            }
        }
    }
}

<强> activity_main.xml中

<?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.navin.dynamicfontchangeapplication.MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Style 1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Style 2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/app_name"/>

</LinearLayout>

我希望这可以帮到你: - )