Android 強制設定橫屏或豎屏 設定全屏

來源:互聯網
上載者:User

標籤:android   blog   http   io   ar   os   java   sp   strong   

內容轉自: http://blog.csdn.net/yuejingjiahong/article/details/6636981

 

Android 強制設定橫屏或豎屏 設定全屏 

全屏

在Activity的onCreate方法中的setContentView(myview)調用之前添加下面代碼

requestWindowFeature(Window.FEATURE_NO_TITLE);//隱藏標題getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);//設定全屏
橫屏

按照下面程式碼範例修改Activity的onResume方法

@Overrideprotected void onResume() { /**  * 設定為橫屏  */ if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } super.onResume();}

或者在設定檔中對Activity節點添加android:screenOrientation屬性(landscape是橫向,portrait是縱向)

android:launchMode="singleTask" android:screenOrientation="portrait">

要設定成豎屏設定成 SCREEN_ORIENTATION_PORTRAIT

// ----------------

常亮

view.setKeepScreenOn(true)

不加任何旋轉螢幕的處理代碼的時候,旋轉螢幕將會導致系統把當前activity關閉,重新開啟。
如果只是簡單的介面調整,我們可以阻止此問題的發生,旋轉螢幕而自己調整螢幕的元素重構。
首先我們需要修改AndroidManifest.xml檔案:
<activity android:name=".Magazine">
</activity>
//修改為:
<activity android:name=".Magazine"
  android:configChanges="orientation|keyboard">
</activity>
這樣是讓程式能夠響應旋轉螢幕的事件。
然後重寫onConfigurationChanged方法:
@Override
public void onConfigurationChanged(Configuration newConfig) {
  // TODO Auto-generated method stub
  super.onConfigurationChanged(newConfig);
  Log.v(" == onConfigurationChanged");
  processLayout();
}

//----------------------------

在我們用Android開發過程中,會碰到Activity在切換到後台或布局從橫屏LANDSCAPE切換到PORTRAIT,會重新切換Activity會觸發一次onCreate方法。

在Android開發中這種情況視可以避免的,我們可以在androidmanifest.xml中的activit元素加入這個屬性 android:configChanges="orientation|keyboardHidden" 就能有效避免oncreat方法的重複載入, 

androidmanifest.xml內容如下:紅色字型為添加部分

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.demo"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".DemoGPS"
            android:configChanges="orientation|keyboardHidden"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  <uses-library android:name="com.google.android.maps" />

    </application>
    <uses-sdk android:minSdkVersion="7" />

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
 
</manifest>

同時在Activity的Java檔案中重載onConfigurationChanged(Configuration newConfig)這個方法,這樣就不會在布局切換或視窗切換時重載等方法。代碼如下:

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

//------------------------------------------------------

關於Android中Activity的橫豎屏切換問題可以通過AndroidManifest.xml檔案中的Activity來配置:
android:screenOrientation=["unspecified" | "user" | "behind" |
"landscape" | "portrait" |
"sensor" | "nonsensor"]
screenOrientation 用來指定Activity的在裝置上顯示的方向,每個值代表如下含義:
"unspecified" 預設值 由系統來判斷顯示方向.判定的策略是和裝置相關的,所以不同的裝置會有不同的顯示方向.
"landscape" 橫屏顯示(寬比高要長)
"portrait" 豎屏顯示(高比寬要長)
"user" 使用者當前首選的方向
"behind" 和該Activity下面的那個Activity的方向一致(在Activity堆棧中的)
"sensor" 有物理的感應器來決定。如果使用者旋轉裝置這螢幕會橫豎屏切換。
"nosensor" 忽略物理感應器,這樣就不會隨著使用者旋轉裝置而更改了 ( "unspecified"設定除外 )。
Android 強制設定橫屏或豎屏 設定全屏

Android 強制設定橫屏或豎屏 設定全屏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.