gpt4 book ai didi

Android:配置更改未更新

转载 作者:太空狗 更新时间:2023-10-29 14:28:46 25 4
gpt4 key购买 nike

我正在使用 XML 标签

android:configChanges="orientation|keyboardHidden|keyboard"

以及以下用于检测设备方向变化和更改布局的代码:

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
disp = getWindowManager().getDefaultDisplay();
swidth = disp.getWidth();
sheight = disp.getHeight();
parent.removeAllViews();
parent = new RelativeLayout(this);
if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
layoutPortrait();
else
layoutLandscape();
}

这从纵向到横向都很好用。但是,无论出于何种原因,从横向到纵向(从横向开始或切换到横向然后再返回)不会将屏幕更改回纵向。

通过使用日志消息,我确定在横向模式后,显示和配置类不会更新。它们保持与设备横向时相同的方向/长度/宽度值。

有人知道这是为什么吗?

附加代码(贡献者要求)

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
disp = getWindowManager().getDefaultDisplay();
swidth = disp.getWidth();
sheight = disp.getHeight();
int ornt;
if(swidth == sheight)
ornt = Configuration.ORIENTATION_SQUARE;
else if(swidth < sheight)
ornt = Configuration.ORIENTATION_PORTRAIT;
else if(swidth > sheight)
ornt = Configuration.ORIENTATION_LANDSCAPE;
else
ornt = Configuration.ORIENTATION_UNDEFINED;
parent = new RelativeLayout(this);
labelOne = new TextView(this);
labelOne.setText("Temperature");
labelOne.setId((int)(Math.random()*Integer.MAX_VALUE));
temp = new EditText(this);
temp.setSingleLine(true);
temp.setBackgroundColor(Color.WHITE);
temp.setTextColor(Color.BLACK);
temp.setId((int)(Math.random()*Integer.MAX_VALUE));
labelTwo = new TextView(this);
labelTwo.setText("Humidity(%)");
labelTwo.setId((int)(Math.random()*Integer.MAX_VALUE));
humidity = new EditText(this);
humidity.setSingleLine(true);
humidity.setBackgroundColor(Color.WHITE);
humidity.setTextColor(Color.BLACK);
humidity.setId((int)(Math.random()*Integer.MAX_VALUE));
output = new TextView(this);
output.setBackgroundColor(Color.WHITE);
output.setTextColor(Color.BLACK);
output.setId((int)(Math.random()*Integer.MAX_VALUE));
submit = new Button(this);
submit.setText("Calculate");
submit.setId((int)(Math.random()*Integer.MAX_VALUE));
submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Double result = Calculate.calculate(getInputs());
if(result !=null) {
String answer = String.format("%,.1f",result);
if(f.isChecked()) {
output.setText(answer + "°F");
} else {
output.setText(answer + "°C");
}
}
}
});
f = new RadioButton(this);
f.setText("Fahrenheit");
c = new RadioButton(this);
c.setText("Celsius");
rg = new RadioGroup(this);
rg.setOnCheckedChangeListener(new ListenForMode());
rg.addView(f);
rg.addView(c);
rg.setId((int)(Math.random()*Integer.MAX_VALUE));
f.setChecked(true);
if(ornt == Configuration.ORIENTATION_PORTRAIT || ornt == Configuration.ORIENTATION_SQUARE)
layoutPortrait();
else
layoutLandscape();
}

最终更新

问题是模拟器没有正确响应方向变化。我更改了 onConfigurationChanged(...) 方向检查以使用条件 width < height 纵向和所有其他横向。这在我的 Android 设备上完美运行。

感谢这个问题的所有贡献者!

最佳答案

您真的不应该覆盖 list 中的 configChanges。 Android 可以为您处理重绘 View ,而且它实际上处理得非常好,尤其是当您在资源文件夹中过滤了布局时。

如果您因为 AsyncTask 失去其上下文的问题(就像我曾经做过的那样)而覆盖 configChanges,我建议您使用 IntentService 来处理您的异步工作,因为服务不依赖于任何一个 Activity 。

关于Android:配置更改未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9087590/

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