Android:配置更改未更新

时间:2012-01-31 21:58:53

标签: android user-interface orientation emulation config

我正在使用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();
}

这种形式从纵向到横向都很好。但是,无论出于何种原因,从风景到肖像(从风景开始或切换到它然后回来)都不会将屏幕改回肖像。

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

有谁知道为什么会这样?

附加代码(供稿人要求)

@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(...)方向检查以使用条件宽度&lt;肖像和所有其他景观的高度。这在我的Android设备上完美运行。

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

2 个答案:

答案 0 :(得分:1)

你真的不应该覆盖清单中的configChanges。 Android可以为您重新处理视图,它实际上处理得非常好,特别是如果您在资源文件夹中有过滤布局。

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

答案 1 :(得分:0)

如果它发生在模拟器上,那就没关系 - 仿真器在配置更改方面表现得有些奇怪。我一直试图解决类似的问题,除非我注意到该应用程序在真实设备上运行良好。

仿真器似乎忽略了android:configChanges="orientation"属性,但有点奇怪:它确实强制配置更改,但每次调用它时都不会调用onConfigurationChanged(..)方法。