【问题标题】:Android: Configuration Change Not UpdatingAndroid:配置更改未更新
【发布时间】:2012-02-23 15:31:57
【问题描述】:

我正在使用 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(...) 方向检查,将条件宽度

感谢所有对此问题的贡献者!

【问题讨论】:

  • 为什么不让 Android 通过提供备用资源来为您处理布局切换?
  • 第二个。另外,您的 layoutLandScape 例程是否会调用 setRequestedOrientation?执行此操作后,您将不再收到有关方向更改的通知。
  • @LawrenceD'Oliveiro 不是它不调用该方法。它只是重新绘制布局以使用横向视图。
  • 您没有回退调用您的布局之一。您应该将if else if 重构为if else。检查纵向,如果没有,横向忽略其余部分...(不会解决您的问题,但应该更健壮)
  • @bschultz 您所说的替代资源是指 BMP GUI 布局吗?我不知道如何制作这些,我宁愿尽可能多地用纯 Java 编写程序,因为这是我所知道和喜爱的。

标签: android user-interface orientation emulation config


【解决方案1】:

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

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

【讨论】:

  • 但是如果我希望它使用不同的布局,我不必重写它吗?请记住,我是在 Java 代码中执行此操作,而不是使用 BMP 资源。
  • 如果您将横向布局放入 layout-land 并保持名称相同,Android 将选择 layout-land 中的布局
  • 您是说您是在使用 Java 代码而不是 XML 构建布局吗?
【解决方案2】:

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

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

【讨论】:

  • 它在模拟器上没有任何作用。我有没有提到它在实际手机上崩溃?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多