在Android Nougat上处理密度/字体大小变化

在Android Nougat上处理密度/字体大小变化,android,fonts,size,display,android-7.0-nougat,Android,Fonts,Size,Display,Android 7.0 Nougat,我想让我的应用程序与Android Nougat兼容,特别是与密度变化和字体大小变化兼容 我在测试中所做的是: 启动应用程序 将其置于多窗口模式(像素C的顶部,纵向模式,在emulator上) 在我的应用程序的列表中放置一些筛选器 使用“设置”应用程序在“我的屏幕”的后半部分更改显示大小或字体大小 如果我没有使用清单中的密度值侦听配置更改,它会重新创建我的活动(这不好,因为我打开了一些筛选器,但丢失了它们)。 如果我听他们说,作为参数传递的newConfig值中的densityDpi值与使用

我想让我的应用程序与Android Nougat兼容,特别是与密度变化和字体大小变化兼容

我在测试中所做的是:

  • 启动应用程序
  • 将其置于多窗口模式(像素C的顶部,纵向模式,在emulator上)
  • 在我的应用程序的列表中放置一些筛选器
  • 使用“设置”应用程序在“我的屏幕”的后半部分更改显示大小或字体大小
如果我没有使用清单中的密度值侦听配置更改,它会重新创建我的活动(这不好,因为我打开了一些筛选器,但丢失了它们)。 如果我听他们说,作为参数传递的newConfig值中的densityDpi值与使用getResources().getConfiguration().densityDpi获得的值相同

我在清单上的当前行如下所示:

android:configChanges=“屏幕大小|最小屏幕大小|屏幕布局|方向|键盘隐藏|密度”

我试过这样的方法:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
  // some code
  if(mOldConfiguration != null && newConfig.densityDpi!= mOldConfiguration.densityDpi){
  // Neither densityDpi nor fontScale works, they are always the same
  }

    if(mOldConfiguration != null && newConfig.fontScale != mOldConfiguration.fontScale){
    // I will later have a method which will recreate the activity with my filters saved
        recreate();
    }
    mOldConfiguration = newConfig;
}
android:configChanges="fontScale"
我已经听过其他配置更改,但不知道如何通过这种方法检测到更改是密度/字体大小的更改

  • 通过在清单文件中添加以下行,可以侦听密度变化:

    android:configChanges="density"
    
    在“活动”中,您可以在进行更改时获取新的配置值:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.e("TRUNGDH", newConfig.densityDpi + "<= densityDpi");
    }
    
    @覆盖
    公共无效OnConfiguration已更改(配置newConfig){
    super.onConfigurationChanged(newConfig);
    Log.e(“TRUNGDH”,newConfig.fontScale+”
    
  • 通过在清单文件中添加以下行,可以侦听密度变化:

    android:configChanges="density"
    
    在“活动”中,您可以在进行更改时获取新的配置值:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.e("TRUNGDH", newConfig.densityDpi + "<= densityDpi");
    }
    
    @覆盖
    公共无效OnConfiguration已更改(配置newConfig){
    super.onConfigurationChanged(newConfig);
    
    Log.e(“TRUNGDH”,newConfig.fontScale+“谢谢你,彭!谢谢你,彭!”!