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);
}