android实现横屏的代码及思路
时间:2023-06-25 09:35:40
当屏幕变为横屏的时候,系统会重新呼叫当前Activity的OnCreate方法,你可以把以下方法放在你的OnCreate中来检查当前的方向,然后可以让你的SetContentView来载入不同的Layout xml.
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.i("info", "landscape");
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.i("info", "portrait");
}
关于屏幕切换的时候
首先需要在androidmanifest.xml中加入配置
android:configChanges="orientation|keyboardHidden|navigation
这样在程序中. Activity就不会重复的调用onCreate()
甚至不会调用onPause.onResume.
只会调用一个onConfigurationChanged(Configuration newConfig)
这是在XML加入配置选项的前提下.
如果在就加入选项的前提下.如上所说. Activity会重新激活onCreate方法
根据你自己的需求来选择配置改变时的处理机制这样比较好一点。
四、java怎样实现ping的功能来确定指定的IP地址是否能连通 可以用InetAddress的isReachable方法:
import java.net.InetAddress;public class MainTest { public static void main(String[] args) {
try {
int timeOut = 3000;
byte[] ip = new byte[] {
(byte) 192, (byte) 168, (byte) 100, (byte) 151 };
int retry = 4; InetAddress address = InetAddress.getByAddress(ip);
for (int i = 0; i < retry; i++) {
if (address.isReachable(timeOut)) {
System.out.println(i + " OK"); }
else {
System.out.println(i + " LOSS");
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android Drawable必备知识小结
![](https://img.aspxhome.com/file/2023/8/138278_0s.jpg)
golang实现java uuid的序列化方法
Java实现map转换成json的方法详解
Flutter Widgets粘合剂CustomScrollView NestedScrollView滚动控件
![](https://img.aspxhome.com/file/2023/9/120089_0s.webp)
Android常用的图片加载库
![](https://img.aspxhome.com/file/2023/8/90168_0s.gif)
Flutter集成高德地图并添加自定义Maker的实践
![](https://img.aspxhome.com/file/2023/4/123234_0s.jpg)
Android提高之TelephonyManager功能探秘
![](https://img.aspxhome.com/file/2023/6/110766_0s.gif)
SpringBoot整合Activiti工作流框架的使用
![](https://img.aspxhome.com/file/2023/2/63842_0s.png)
java -jar设置添加启动参数实现方法
c++指针使用形参改变实参的方法
C#中读取App.config配置文件代码实例
C#委托初级使用的实例代码
C# TrackBar拖动条改变滑块颜色
![](https://img.aspxhome.com/file/2023/5/122695_0s.gif)
C#中数组、ArrayList和List三者的区别详解及实例
Android Studio实现带边框的圆形头像
![](https://img.aspxhome.com/file/2023/6/92576_0s.png)
mybatis中查询结果为空时不同返回类型对应返回值问题
解决JAVA遍历List集合,删除数据时出现的问题
Android 屏幕切换监听的实例代码
详解java开发webservice的几种方式
同时使用@LoadBalanced @RefreshScope注解负载均衡失效分析
![](https://img.aspxhome.com/file/2023/7/62037_0s.png)