Android橫豎屏切換屬性
Android橫豎屏切換通過在AndroidManifest.xml中設定activity中的android:screenOrientation屬性值來實現。
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:screenOrientation="sensor"
android:name="com.qiubai00.fragment01.MainActivity"
android:label="@string/app_name" >
該android:screenOrientation屬性,他有以下幾個參數:
"unspecified":預設值 由系統來判斷顯示方向.判定的策略是和裝置相關的,所以不同的裝置會有不同的顯示方向.
"landscape":橫屏顯示(寬比高要長)
"portrait":豎屏顯示(高比寬要長)
"user":使用者當前首選的方向
"behind":和該Activity下面的那個Activity的方向一致(在Activity堆棧中的)
"sensor":有物理的感應器來決定。如果使用者旋轉裝置這螢幕會橫豎屏切換。
"nosensor":忽略物理感應器,這樣就不會隨著使用者旋轉裝置而更改了("unspecified"設定除外)。
比如下列設定
android:screenOrientation="portrait"
則無論手機如何變動,擁有這個屬性的activity都將是豎屏顯示。
android:screenOrientation="landscape",為橫屏顯示。
上述修改也可以在Java代碼中通過類似如下代碼來設定
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
//在MainActivity中擷取螢幕是橫屏還是豎屏:
DisplayMetrics dm=new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
Fragment1 fragment1=new Fragment1();
Fragment2 fragment2=new Fragment2();
FragmentManager fm=this.getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
if(width>height)//橫屏