Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于APP内切换语言后,切换横竖屏时,AutoSize失效的一种解决方案 #465

Closed
getglory opened this issue Nov 6, 2024 · 0 comments

Comments

@getglory
Copy link

getglory commented Nov 6, 2024

在onConfigurationChanged中设置完语言后,将设置语言时返回的context的resource传递给AutoSizeCompat,即可保证二者使用同一个resource,避免其中一方失效的问题。根据自己的需求调用autoConvertDensity传需要的宽高和是否根据宽度适配等待。参考:#13 (comment)

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    String language = AppConfig.INSTANCE.getAppLanguage();
    // 对应语言环境下的context
    Context context = LanguageManager.INSTANCE.setLocale(this, language);
    //需要升级到 v1.1.2 及以上版本才能使用 AutoSizeCompat
    //修复设置语言后,Autosize失效的问题
    if (newConfig.orientation != Configuration.ORIENTATION_LANDSCAPE) {
        // 纵向
        AutoSizeCompat.autoConvertDensity(context.getResources(), 0, true);//如果有自定义需求就用这个方法
    } else {
        // 横向
        AutoSizeCompat.autoConvertDensity(context.getResources(), 1280, true);//如果有自定义需求就用这个方法
    }
}

@Override
public void attachBaseContext(Context newBase ) {
    String language = AppConfig.INSTANCE.getAppLanguage();
    // attach对应语言环境下的context
    Context context = LanguageManager.INSTANCE.setLocale(newBase , language);
    super.attachBaseContext(context);
}
@getglory getglory closed this as completed Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant