首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android自适应荧屏方向和大小

2012-09-09 
android自适应屏幕方向和大小manifest xmlns:androidhttp://schemas.android.com/apk/res/android ? ?

android自适应屏幕方向和大小
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
? ?? ?package="com.ray.linkit"
? ?? ?android:versionCode="1"
? ?? ?android:versionName="1.0">
? ? <application android:icon="@drawable/icon" android:label="@string/app_name">
? ?? ???<activity android:name=".Main"
? ?? ?? ?? ?? ?? ?android:label="@string/app_name"
? ?? ?? ?? ?? ?? ?android:screenOrientation="portrait">
? ?? ?? ?? ?<intent-filter>
? ?? ?? ?? ?? ? <action android:name="android.intent.action.MAIN" />
? ?? ?? ?? ?? ? <category android:name="android.intent.category.LAUNCHER" />
? ?? ?? ?? ?</intent-filter>
? ?? ???</activity>
? ?? ?? ?? ?? ? <activity android:name=".GamePlay"
? ?? ?? ?? ?? ? android:screenOrientation="portrait"></activity>
? ?? ?? ?? ?? ? <activity android:name=".Option? ?? ?? ?? ?? ? android:screenOrientation="portrait"></activity>
? ? </application>
? ? <uses-</manifest>

另外,android中每次屏幕的切换动会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次Create的时候载入配置,那样,进行中的游戏就不会自动重启了!

有的程序适合从竖屏切换到横屏,或者反过来,这个时候怎么办呢?可以在配置Activity的地方进行如下的配置android:screenOrientation="portrait"。这样就可以保证是竖屏总是竖屏了,或者landscape横向。

而有的程序是适合横竖屏切换的。如何处理呢?首先要在配置Activity的时候进行如下的配置:android:configChanges="keyboardHidden|orientation",另外需要重写Activity的onConfigurationChanged方法。实现方式如下,不需要做太多的内容:

@Override
? ?? ???public void onConfigurationChanged(Configuration newConfig) {
? ?? ?? ?? ?? ? super.onConfigurationChanged(newConfig);
? ?? ?? ?? ?? ? if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
? ?? ?? ?? ?? ?? ?? ?? ?// land do nothing is ok
? ?? ?? ?? ?? ? } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
? ?? ?? ?? ?? ?? ?? ?? ?// port do nothing is ok
? ?? ?? ?? ?? ? }
? ?? ???}

热点排行