Android橫豎屏切換屬性

來源:互聯網
上載者:User

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)//橫屏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.