gpt4 book ai didi

java - 无法正常工作 AttachBaseContext

转载 作者:行者123 更新时间:2023-12-02 00:50:45 24 4
gpt4 key购买 nike

请告诉我,我在android 7及以上版本上遇到了一个问题,如果我在attachBaseContext中制作固定字体,那么当我旋转屏幕时,屏幕的宽度和高度不确定,它总是相同的。如何解决这个问题,或者代码有什么问题?

ps。你可以在模拟器上看这段代码,在Android 7及更高版本上,当你旋转屏幕时,你会看到Toast中的宽度和高度没有改变。

list :

android:configChanges="orientation|screenSize"

代码:

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
final Configuration override = new Configuration(newBase.getResources().getConfiguration());
override.fontScale = 1.3f;// fixed font
applyOverrideConfiguration(override);
}



//track screen rotation
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Toast.makeText(this, "w - " + String.valueOf(this.getWindowManager().getDefaultDisplay().getWidth()) + "\n" +
"h - " + String.valueOf(this.getWindowManager().getDefaultDisplay().getHeight())
, Toast.LENGTH_SHORT).show();
}

最佳答案

您可以覆盖所有配置,而不仅仅是 fontScale。

final Configuration override = new Configuration();
override.fontScale = 1.3f;// fixed font
applyOverrideConfiguration(override);

据官方docs

A Configuration specifying what values to modify in the base Configuration of the original Context's resources. If the base configuration changes (such as due to an orientation change), the resources of this context will also change except for those that have been explicitly overridden with a value here.

关于java - 无法正常工作 AttachBaseContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57860785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com