反射方式修改App字体

  1. 自定义Applicaiton,实现全局App修改,对于字体也是同理,通过Typeface设置加载App全局字体,对于自定义的Applicaiton需在AndroidManifest.xml的<application>tag这个标签中进行设置.

         public class App extends Application {
         private static final String TAG = "软件商店";
             public static Typeface typeFace;
    
         @Override
         public void onCreate() {// Application启动时调用
     // TODO Auto-generated method stub
             super.onCreate();
             setTypeface();
         }
             public void setTypeface() {
     // 微软雅黑,加载外部字体assets/front/huawen_caiyun.ttf
         typeFace = Typeface.createFromAsset(getAssets(), "fonts/mi_font.ttf");
         try {
                 Field field_3 = Typeface.class.getDeclaredField("SANS_SERIF");
                 field_3.setAccessible(true);
                 field_3.set(null, typeFace);
                     } catch (NoSuchFieldException e) {
                         e.printStackTrace();
                 } catch (IllegalAccessException e) {
                     e.printStackTrace();
         }
      }
     }
    
  2. 自定义Applicaiton的Theme,在Values文件夹下style文件下自定义Theme,并且设置<item name="android:typeface">sans</item>属性

     <style name="AppTheme" parent="@android:style/Theme.NoTitleBar">
          <item name="android:typeface">sans</item>
          <!-- All customizations that are NOT specific to a particular API-level can go here. -->
     </style>
    

    这里android:typeface可以设置的仅仅有normal、sans、serif、monospace可以设置,因为我在SetAppTypeface类中设置的是Typeface.class.getDeclaredField("SANS_SERIF");所以我这里便设置成sans,如果getDeclaredField()设置的是其他的类型,则要选择同类型的其他诸如serif、monospace等等

  3. 在AndroidManifest.xml的<application>标签中使用自定义Theme

     <application
     android:allowBackup="true"
     android:name=".App"
     android:icon="@drawable/ic_launcher"
     android:label="@string/app_name"
     android:theme="@style/AppTheme" >
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
         <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         </activity>
     </application>  
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,510评论 0 17
  • 首先在自定义的BaseApplication中设置如下代码: public class BaseApplicati...
    诠_释阅读 837评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,890评论 18 139
  • 前言近期项目需要在我们的APP中使用指定的字体库。经过搜集资料,研读源码,和别人探讨请教,最终产出了一些比较好的方...
    肖丹晨阅读 3,953评论 0 6
  • 问题背景 之前在编写鲤鱼日语时,因为使用了外部的字体,这导致了一个问题就是我的ListView显示的是圆滑的字体,...
    老卫阅读 5,052评论 4 20