在Android中更改整个应用程序的字体

时间:2015-01-26 09:38:59

标签: android fonts

所以我在问这个问题之前先做了一些研究,因为我知道有很多关于这个问题的文章。 但我发现有关更改整个应用程序字体的解决方案仅限于textview,而不是例如操作栏和菜单。我也知道如何更改这些字体(在每个活动中),但这是在运行时完成的,所以当我使用下面的代码来更改菜单项的字体时,我注意到文本从默认字体更改到自定义字体。 我的问题是,有没有更好的方法来做到这一点

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.mainpage, menu);


        getLayoutInflater().setFactory(new Factory() {
            public View onCreateView(String name, Context context,
                                     AttributeSet attrs) {

                if (name.equalsIgnoreCase("TextView")) {
                    try {
                        LayoutInflater li = LayoutInflater.from(context);
                        final View view = li.createView(name, null, attrs);
                        new Handler().post(new Runnable() {
                            public void run() {

                                if(MainActivitySharedPref.GetValue(getApplicationContext(), "Language").equals("ar")) {
                                    Typeface font = Typeface.createFromAsset(getAssets(), getString(R.string.arabic_font_name));
                                    ((TextView) view).setTypeface(font);
                                    ((TextView) view).setTextSize(22);
                                }
                            }});
                        return view;
                    } catch (Exception e) {Log.e(TAG, "[ERROR] an InflateException was thrown: " + e.getMessage());}
                }
                if (name.equalsIgnoreCase("Button")) {
                    try {
                        LayoutInflater li = LayoutInflater.from(context);
                        final View view = li.createView(name, null, attrs);
                        new Handler().post(new Runnable() {
                            public void run() {

                                if(MainActivitySharedPref.GetValue(getApplicationContext(), "Language").equals("ar")) {
                                    Typeface font = Typeface.createFromAsset(getAssets(), getString(R.string.arabic_font_name));
                                    ((Button) view).setTypeface(font);
                                    ((Button) view).setTextSize(22);
                                }
                            }
                        });
                        return view;
                    } catch (Exception e) { Log.e(TAG, "[ERROR] an InflateException was thrown: " + e.getMessage());}
                }
                return null;
            }

        });

        return true;
    }

1 个答案:

答案 0 :(得分:0)

由于我没有回答,我将发布我所做的解决在自定义字体出现之前加载默认字体毫秒的问题。 (在菜单项中......) 我将代码放在OnCreate方法中并删除了运行函数。

        LayoutInflater layoutInflater = getLayoutInflater();
        final LayoutInflater.Factory existingFactory = layoutInflater.getFactory();
        // use introspection to allow a new Factory to be set
        try {
            Field field = LayoutInflater.class.getDeclaredField("mFactorySet");
            field.setAccessible(true);
            field.setBoolean(layoutInflater, false);
            getLayoutInflater().setFactory(new LayoutInflater.Factory() {
                public View onCreateView(String name, Context context,
                                         AttributeSet attrs) {

                    if (name.equalsIgnoreCase("TextView")) {
                        try {

                            LayoutInflater li = LayoutInflater.from(context);
                            final View view = li.createView(name, null, attrs);
//                            new Handler().post(new Runnable() {
//                                public void run() {

                            if (MainActivitySharedPref.GetValue(getApplicationContext(), "Language").equals("ar")) {
                                Typeface font = Typeface.createFromAsset(getAssets(), getString(R.string.arabic_font_name));

                                ((TextView) view).setTypeface(font);
                                ((TextView) view).setTextSize(22);
                            }
//                                }
//                            });
                            return view;
                        } catch (Exception e) {
                            Log.e(TAG, "[ERROR] an InflateException was thrown: " + e.getMessage());
                        }
                    }

                    if (name.equalsIgnoreCase("Button")) {
                        try {
                            LayoutInflater li = LayoutInflater.from(context);
                            final View view = li.createView(name, null, attrs);
//                            new Handler().post(new Runnable() {
//                                public void run() {
//                                    // set the text color
                            if (MainActivitySharedPref.GetValue(getApplicationContext(), "Language").equals("ar")) {
                                Typeface font = Typeface.createFromAsset(getAssets(), getString(R.string.arabic_font_name));
                                ((Button) view).setTypeface(font);
                                ((Button) view).setTextSize(22);
                            }
//                                }
//                            });
                            return view;
                        } catch (Exception e) {
                            Log.e(TAG, "[ERROR] an InflateException was thrown: " + e.getMessage());
                        }
                    }
                    return null;
                }

            });
        }catch(Exception e){}