《》如果你所用的Android手機沒有將屏幕方向鎖定的話,那么所有的應用程序的界面都會有隨着屏幕的方向的改變而發生改變,如果你的應用程序的界面沒有針對屏幕的改變而在代碼上做出一些適應的操作的話,那么手機就會以硬性的方式將應用程序界面強行適應屏幕的方向,但是這時就有可能使界面變得非常的丑;下面就介紹一些常用的有關屏幕方向改變的方法
1、強行設定應用程序的顯示方向
也就說,我們可以指定應用程序不理睬屏幕的旋轉,這是在Manifest.xml文件中配置Activity的時候指定的,
<activity>節點的android:screenOrientation屬性可以完成該項任務,示例代碼如下:
<activity android:name=".EX01" android:label="@string/app_name" android:screenOrientation="portrait"> // 值為portrait時強制為豎屏, 值為landscape時強制為橫屏 </activity>
當然我們也可以在onCreate方法中的靠前位置,使用java代碼來設定
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATTION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATTION_PORTRAIT);
2、強制開啟屏幕旋轉效果
如果用戶的手機沒有開啟重力感應器或者在AndroidManifest.xml中設置了android:screenOrientation,默認情況下,該Activity不會響應屏幕旋轉事件。如果在這種情況下,依然希望Activity能響應屏幕旋轉,則添加如下代碼:
// 在activity的 onCreate 函數中添加
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
3、監聽屏幕旋轉
雖然上面的前言部分已經說明,屏幕旋轉時會硬性的使應用程序適應屏幕方向,但是實際上不是這么簡單,實際上當屏幕發生旋轉(當手機的方向鎖沒有開啟,並且沒用強行設定屏幕顯示方向),那么程序是上將會做以下幾個步驟:
①調用當前的Activity的onSaveInstanceState(Bundle outState)去保存應用中的一些數據,你可以重寫這個方法
②然后調用onDestroy()銷毀當前的Activity
③重新調用onCreate()或onRestoreInstanceState()方法去重新創建一個Activity(也就是說,程序會重新將onCreate中的代碼在執行一遍)
-----好了,你想如果我們的onCreate方法中沒有任何的處理的話,那么就會產生所謂的硬性的適應問題,但是我們可以在寫onCreate的代碼的時候,就分成兩部分,也就是說:我們先創建兩個布局文件,一個是在橫屏的狀態下使用的,一個是在豎屏狀態下使用的,好了,之后寫onCreate的代碼的時候首先判斷當前的屏幕的方向,如果是橫屏的話,那么就使用橫屏布局文件,如果是豎屏的話,就使用豎屏布局文件,那么這個問題就解決了
-----除此之外,還要說明的一點是onSaveInstanceState(Bundle outState)到底怎么用:當屏幕發生旋轉的時候會調用這個方法,這里的參數Bundle,就是讓我們來封裝在屏幕 未旋轉前時想要保存的數據的,之后調用onDestroy()銷毀當前的Activity,最后重新調用onCreate(Bundle savedInstanceState),這時outState就會賦給savedInstanceState,這樣的話,我們在onCreate中重新創建Activity的時候,可以用之前保存的數據來初始化重新創建的Activity,這很重要,因為一個應用程序在整個運行過程中,不論在橫屏和豎屏,數據的變化都要有一致性,不能因為屏幕發生改變,就讓用戶從頭開始重新操作一遍
-----實際上,我們如果是為了保存用戶之前操作的數據的話,那么我們通常不用Bundle來封裝,只要把那些和界面相關的數據的組件設為Activity的全局變量,就沒有問題了,至於那些提交數據,直接提交數據庫就Ok了,但是有上面的用法,不要忘了;
-----除此之外,由於屏幕旋轉時,一定會觸發onSaveInstanceState(Bundle outState)方法,那么我們完全可以將這個方法作為屏幕旋轉的監聽器
下面就舉一個例子:
注意判斷橫屏和豎屏並不是簡單的事,因為Android中規定,只有是完全豎和橫才能夠返回ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE、ActivityInfo.SCREEN_ORIENTATION_PORTRAIT 而這在實際中幾乎無法存在這樣的情況,所以我們特別寫了一個判定屏幕方向的方法
activity_orientation1.xml文件 , 當豎屏時,加載這個布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.penglee.screenorientation.OrientationActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="現在是豎屏" /> </RelativeLayout>
activity_orientation2.xml文件 , 當橫屏時,加載這個布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="現在是橫屏" /> </LinearLayout>
OrientationActivity.java文件
public class OrientationActivity extends Activity { final String LOG = "------ScreenOrientation------" ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //強行開啟屏幕旋轉效果 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR); if(savedInstanceState == null){ setContentView(R.layout.activity_orientation1); } if(savedInstanceState != null){ //橫屏 if( ScreenOrient(this)==ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ) setContentView(R.layout.activity_orientation1); //豎屏 if( ScreenOrient(this)==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ) setContentView(R.layout.activity_orientation2); String temp = savedInstanceState.getString("data_key") ; Log.d(LOG , "重新創建了Activity,之前保存的內容是"+temp) ; } } //判定當前的屏幕是豎屏還是橫屏 public int ScreenOrient(Activity activity) { int orient = activity.getRequestedOrientation(); if(orient != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE && orient != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { WindowManager windowManager = activity.getWindowManager(); Display display = windowManager.getDefaultDisplay(); int screenWidth = display.getWidth(); int screenHeight = display.getHeight(); orient = screenWidth < screenHeight ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } return orient; } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); String tempData = "Something you want to save"; outState.putString("data_key", tempData); Log.d(LOG, "onSaveInstanceState.."); } }
好了大功告成!還有一些小的方法之后在說
敬請關注:http://group.cnblogs.com/27030/