解决系统改变字体大小的时候导致的界面布局混乱的问题

来源:互联网 发布:java字符串切割 编辑:程序博客网 时间:2024/04/30 01:35

转自:http://www.android100.org/html/201401/13/5330.html

从android4.0起系统设置的”显示“提供设置字体大小的选项。这个设置直接会影响到所有sp为单位的字体适配,所以很多app在设置了系统字体后瞬间变得面目全非。下面是解决方案

 
Resources res = getResources();
Configuration config=new Configuration();
config.setToDefaults();
res.updateConfiguration(config,res.getDisplayMetrics() );
 
虽然google推荐使用sp作为字体的单位,但实际的开发过程中通常是根据UIUE的设计稿来换算 sp(px换算sp)。而sp即使在同一种密度下其值也不尽相同。比如在240dpi的设备,如果是480x800分辨率这个值通常是1.5倍 (scaledDensity=1.5),如果是480xZ(z>800)那么这个值有可能大于1.5。这无疑给设备的适配带来更多的困难和陷阱。所以个人通常建议使用dpi来作为字体的单位

对于个别app不需要根据系统字体的大小来改变的,可以在activity基类(app中所有的activity都应该有继承于我们自己定义的一个activity类)中加上以下code:
例如:Contacts中需要在com.android.contacts.activities.TransactionSafeActivity加入以下code
    @Override
    public Resources getResources() {
        Resources res = super.getResources();  
        Configuration config=new Configuration();  
        config.setToDefaults();  
        res.updateConfiguration(config,res.getDisplayMetrics() );
        return res;
    }
如果app中只是个别界面不需要,可改造下此方法
 @Override
    public Resources getResources() {
        if(isNeedSystemResConfig()){
            return super.getResources();
        }else{
            Resources res = super.getResources();  
            Configuration config=new Configuration(); 
            config.setToDefaults(); 
            res.updateConfiguration(config,res.getDisplayMetrics() );
            return res;
        }
    }
    // 默认返回true,使用系统资源,如果个别界面不需要,在这些activity中Override this method ,then return false;
    protected boolean isNeedSystemResConfig() {
        return true;
    }
0 0