簡體   English   中英

如何為手機和平板電腦設置screenOrientation

[英]How to set screenOrientation for phone and tablet

我的manifest.xml如下:

    <activity
        android:name="packagename"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:screenOrientation="sensor" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

然后在活動中創建onCreate,如下所示:

    TelephonyManager manager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
    if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {
        //Tablet
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }
    else {
        //Mobile
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    }

我在manifest.xml中設置了默認的SCREEN_ORIENTATION_SENSOR。
並在onCreate中判斷手機或平板電腦。
但是它可能首先顯示LANDSCAPE,然后在電話設備上更改為PORTRAIT。
我不想一開始就顯示LANDSCAPE。
我該怎么做?

將對setContentView()的調用推遲到執行以下操作:

TelephonyManager manager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {
    //Tablet
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
else {
    //Mobile
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}

// Set content View
setContentView(R.layout.whatever);

// Or
setContentView(mContentView);

將此行添加到manifest.xml文件中。

<activity android:name=".activity"
  android:screenOrientation="portrait">
</activity>

刪除與您編寫的方向相關的所有內容,然后添加此內容

android:screenOrientation="portrait"

對你的活動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM