软件编程
位置:首页>> 软件编程>> Android编程>> android实现横屏的代码及思路

android实现横屏的代码及思路

  发布时间:2023-06-25 09:35:40 

标签:android,横屏

当屏幕变为横屏的时候,系统会重新呼叫当前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(); 

}

}

}


0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com