我们知道 android在屏幕旋转以后,有些情况会销毁当前的Activity. 也就是说从新启动个Activity. 然后 跑onCreate() onStart()等流程.
还有中情况是 屏幕旋转后,不从新启动这个Activity. 只做些动作.
第一中情况是正常情况的流程. 第二中情况是你在manifest.xml里注册了android:config后的动作,你告诉了喜欢,当哪写配置改变后会做什么.
那如何动态检测当前屏幕方向及大小呢?
public class orientation extends Activity {
TextView textview=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textview=(TextView)findViewById(R.id.textview);
int screenwidth=getWindowManager().getDefaultDisplay().getWidth();
int screenheight=getWindochanger codewManager().getDefaultDisplay().getHeight();
Log.d("orient", "now the screen width is :"+screenwidth+"screen height is:"+screenheight);
int orient=getRequestedOrientation();
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Log.d("orient", "now the screen orient is landscape");
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
Log.d("orient", "now the screen orient is portrait");
}
}
}
哎,这样就很明显了,问题的根源是配置改变,那就找配置信息.getResources().getConfiguration即可获得当前配置对象.
这样读就可以了.
当然getWindowManager().getDefaultDisplay().getHeight() getWidth()就获得了高度,宽度.获得值会随屏幕方向改变而改变.