2.随屏幕旋转时,不重新调用onCreate。
当将手机屏幕旋转时,系统会被强制重置启动onCreate方法。
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);
}
Android 禁止屏幕旋转 & 旋转屏幕时保持Activity内容,布布扣,bubuko.com
Android 禁止屏幕旋转 & 旋转屏幕时保持Activity内容
原文:http://www.cnblogs.com/zmc/p/3638256.html