国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

android橫豎屏切換不重啟activity解決方案

2020-04-11 12:40:53
字體:
來源:轉載
供稿:網友
部分網友會發(fā)現(xiàn)Activity在切換到后臺或布局從橫屏LANDSCAPE切換到PORTRAIT,會重新切換Activity會觸發(fā)一次onCreate方法,我們可以在androidmanifest.xml中的activit元素加入這個屬性Android:configChanges="orientation|keyboardHidden" 即可,比如
<activity android:name=".android123" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name">
Java代碼
復制代碼 代碼如下:

/* 聲明Display對象,以取得屏幕寬高 */
final Display defaultDisplay = getWindow().getWindowManager()
.getDefaultDisplay();

intScreenH = defaultDisplay.getHeight();
intScreenW = defaultDisplay.getWidth();

/* 如果為Landscape */
if (intScreenW > intScreenH)
{
/* Landscape => Portrait */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else
{
/* Portrait => Landscape */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
/* 聲明Display對象,以取得屏幕寬高 */
final Display defaultDisplay = getWindow().getWindowManager()
.getDefaultDisplay();
intScreenH = defaultDisplay.getHeight();
intScreenW = defaultDisplay.getWidth();
/* 如果為Landscape */
if (intScreenW > intScreenH)
{
/* Landscape => Portrait */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else
{
/* Portrait => Landscape */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

同時在Activity的Java文件中重載onConfigurationChanged(Configuration newConfig)這個方法,這樣就不會在布局切換或窗口切換時重載onCreate等方法。代碼如下:
Java代碼
復制代碼 代碼如下:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//land
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
//port
}
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//land
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
//port
}
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 广安市| 迭部县| 衡阳市| 微博| 吉安县| 红桥区| 沙河市| 广安市| 景泰县| 普陀区| 丰顺县| 财经| 柘城县| 忻城县| 迁安市| 同德县| 桃园市| 道孚县| 南城县| 高陵县| 宁阳县| 玉山县| 密云县| 扬中市| 唐海县| 佛山市| 铁岭县| 乐昌市| 南部县| 福州市| 辉县市| 孙吴县| 宜宾市| 同心县| 永州市| 来宾市| 阿合奇县| 奎屯市| 洪湖市| 大竹县| 大兴区|