2012-01-31 103 views
3

我使用的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设备。

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

+0

为什么不让Android通过提供备用资源来处理布局切换? – bschultz 2012-01-31 23:35:54

+0

其次。另外,您的layoutLandScape例程是否可以调用setRequestedOrientation?一旦你这样做,你将停止获取方向变化的通知。 – 2012-02-01 02:53:19

+0

@ LawrenceD'Oliveiro不是它不叫这种方法。它只是重新绘制布局以使用横向视图。 – bgroenks 2012-02-05 17:53:53

回答

0

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

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

+0

它不会在模拟器上做任何事情。我有没有提到它在实际的手机上崩溃? – bgroenks 2012-02-08 20:07:53

+0

好吧,划伤崩溃。这是测试代码,没有被删除... – bgroenks 2012-02-08 20:12:28

1

你真的不应该重写清单中的configChanges。 Android可以为你处理重绘视图,并且它可以很好地处理它,特别是如果你已经在资源文件夹中过滤了布局。

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

+0

但是如果我想让它使用不同的布局,我不必重写它吗?请记住,我在Java代码中使用BMP资源。 – bgroenks 2012-02-06 03:29:32

+0

如果你把你的风景布局放在layout-land中,并保留这些名字,相同的Android将选择layout-land中的布局 – SeanPONeil 2012-02-06 17:04:12

+0

你是说你用Java代码而不是XML来构建你的布局? – SeanPONeil 2012-02-06 17:04:48