android横竖屏切换不重启activity解决方案
时间:2022-09-04 06:57:56
部分网友会发现Activity在切换到后台或布局从横屏LANDSCAPE切换到PORTRAIT,会重新切换Activity会触发一次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
}
}
标签:android横竖屏切换,activity
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# 递归算法详解
2021-09-10 19:08:44
C#读取XML的三种实现方式
2023-02-08 10:07:01
![](https://img.aspxhome.com/file/2023/8/102178_0s.png)
详解IDEA中SpringBoot整合Servlet三大组件的过程
2023-05-06 15:23:13
关于Java从本地文件复制到网络文件上传
2022-05-10 16:49:02
![](https://img.aspxhome.com/file/2023/6/98126_0s.jpg)
Mybatis日志模块的适配器模式详解
2023-11-26 12:45:32
Spring Bean初始化及销毁多种实现方式
2023-06-10 13:14:01
![](https://img.aspxhome.com/file/2023/0/71620_0s.jpg)
C#实现快速排序算法
2023-03-20 08:41:51
![](https://img.aspxhome.com/file/2023/8/84478_0s.jpg)
Struts2中Action三种接收参数形式与简单的表单验证功能
2022-11-26 08:55:20
Java环境配置原理全面解析
2023-11-23 07:56:48
![](https://img.aspxhome.com/file/2023/5/59925_0s.jpg)
解决IntelliJ IDEA中鼠标拖动选择为矩形区域问题
2022-04-03 21:08:51
![](https://img.aspxhome.com/file/2023/6/65176_0s.png)
SpringBoot绿叶显示yml和端口问题及解决方法
2023-12-09 00:29:13
![](https://img.aspxhome.com/file/2023/5/62775_0s.png)
Android Service总结及详细介绍
2023-11-15 14:19:43
![](https://img.aspxhome.com/file/2023/0/113300_0s.png)
C#实现调用本机摄像头实例
2022-07-01 19:54:49
智能指针与弱引用详解
2023-05-10 04:45:56
一步步教你如何使用Java实现WebSocket
2023-06-07 04:18:20
![](https://img.aspxhome.com/file/2023/1/77971_0s.png)
C#生成EMF矢量图形文件示例详解
2022-10-30 02:12:56
![](https://img.aspxhome.com/file/2023/9/79129_0s.jpg)
springboot实现返回文件流
2023-04-04 22:19:22
C#将HashTable中键列表或值列表复制到一维数组的方法
2022-01-05 22:12:12
优化SpringBoot程序启动速度的实现
2023-09-28 13:04:52
![](https://img.aspxhome.com/file/2023/7/68727_0s.jpg)
Android Service详解及示例代码
2021-12-24 05:44:17
![](https://img.aspxhome.com/file/2023/7/88667_0s.jpg)