Android 禁止屏幕旋转 & 旋转屏幕时保持Activity内容
2.随屏幕旋转时,不重新调用onCreate。
当将手机屏幕旋转时,系统会被强制重置启动onCreate方法。
- android:configChanges,这个方法主要是负责列出清单,当清单上用户指定的设置改变时,Activity会自己处理这些变化。
- orientation,屏幕界面旋转(可能是用户手动旋转的),【注意:如果你的开发API等级等于或高于13,你还需要设置screenSize,因为screenSize会在屏幕旋转时改变】
- keyboardHidden,键盘辅助功能改变
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
if (newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE) {
// Nothing need to be done here } else {
// Nothing need to be done here
} }
Java代码
/**
* 屏幕改变时自动调用
* @param widthMeasureSpec 改变后的宽度
* @param heightMeasureSpec 改变后的高度
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
/*宽度*/
int screenWith = View.MeasureSpec.getSize(widthMeasureSpec);
/*高度*/
int screenHeight = View.MeasureSpec.getSize(heightMeasureSpec);
/*竖直布局*/
if (screenWith < screenHeight)
{
this.setOrientation(VERTICAL);
for (int i = 0; i < getChildCount(); i++)
{
View childView = getChildAt(i);
if (childView instanceof CakyCanvas)
{
/*该控件占布局的2/5*/
LayoutParams params = new LayoutParams(screenWith,
screenHeight * 2/ 5
updateViewLayout(childView, params);
}
else if (childView instanceof CakyExplainCanvas)
{
/*该控件占布局的3/5*/
LayoutParams params = new LayoutParams(screenWith,
screenHeight * 3/ 5
updateViewLayout(childView, params);
}
}
}
/*横向布局*/
else
{
this.setOrientation(HORIZONTAL);
for (int i = 0; i < getChildCount(); i++)
{
View childView = getChildAt(i);
if (childView instanceof CakyCanvas)
{
LayoutParams params = new LayoutParams(
screenWith * 2/ 5
screenHeight);
updateViewLayout(childView, params);
}
else if (childView instanceof CakyExplainCanvas)
{
LayoutParams params = new LayoutParams(
screenWith * 3/ 5
screenHeight);
updateViewLayout(childView, params);
}
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}