android activity landscape,強制Android活動始終使用橫向模式

I am using the Android VNC viewer on my HTC G1. But for some reason, that application is always in landscape mode despite my G1 is in portrait mode. Since the Android VNC viewer is open source, I would like know how is it possible hard code an activity to be 'landscape'. I would like to change it to respect the phone orientation.

我在HTC G1上使用Android VNC查看器。但是由於某些原因,這個應用程序總是處於橫向模式,盡管G1處於縱向模式。由於Android VNC查看器是開源的,我想知道硬編碼活動如何成為“景觀”。我想改變一下,以尊重手機的定位。

77132b3f745bfd8c52c763923d1c0862.png

14 个解决方案

#1

171

Looking at the AndroidManifest.xml (link), on line 9:

看AndroidManifest。xml(鏈接),第9行:

This line specifies the screenOrientation as landscape, but author goes further in overriding any screen orientation changes with configChanges="orientation|keyboardHidden". This points to a overridden function in VncCanvasActivity.java.

這一行將屏幕方向指定為橫向,但作者在使用configChanges=“orientation|keyboardHidden”重寫屏幕方向更改時做了進一步的修改。這指向了VncCanvasActivity.java中的一個覆蓋函數。

If you look at VncCanvasActivity, on line 109 is the overrided function:

如果你看看VncCanvasActivity,第109行是重寫函數:

@Override

public void onConfigurationChanged(Configuration newConfig) {

// ignore orientation/keyboard change

super.onConfigurationChanged(newConfig);

}

The author specifically put a comment to ignore any keyboard or orientation changes.

作者特別添加了一個注釋來忽略任何鍵盤或方向的改變。

If you want to change this, you can go back to the AndroidManifest.xml file shown above, and change the line to:

如果你想改變這一點,你可以回到AndroidManifest。上面顯示的xml文件,並將該行更改為:

This should change the program to switch from portrait to landscape when the user rotates the device.

當用戶旋轉設備時,應該將程序從縱向切換到橫向。

This may work, but might mess up how the GUI looks, depending on how the layout were created. You will have to account for that. Also, depending on how the activities are coded, you may notice that when screen orientation is changed, the values that were filled into any input boxes disappear. This also may have to be handled.

這可能有用,但可能會弄亂GUI的外觀,這取決於創建布局的方式。你必須考慮到這一點。另外,根據活動的編碼方式,您可能會注意到,當屏幕方向發生更改時,填充到任何輸入框中的值將消失。這也需要處理。

#2

109

You can set the same data in your java code as well.

您還可以在java代碼中設置相同的數據。

myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Other values on ActivityInfo will let you set it back to sensor driven or locked portrait. Personally, I like to set it to something in the Manifest as suggested in another answer to this question and then change it later using the above call in the Android SDK if there's a need.

ActivityInfo上的其他值將允許您將其設置為傳感器驅動或鎖定人像。就我個人而言,我喜歡將它設置為清單中針對這個問題的另一個答案所建議的內容,然后在需要時使用Android SDK中的上述調用進行更改。

#3

34

In my OnCreate(Bundle), I generally do the following:

在我的OnCreate(Bundle)中,我通常做以下事情:

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

#4

30

You can specify the orientation of an activity in the manifest. See here.

您可以在清單中指定活動的方向。在這里看到的。

...

android:screenOrientation=["unspecified" | "user" | "behind" |

"landscape" | "portrait" |

"sensor" | "nosensor"]

...

"adjustResize", "adjustPan"] >

#5

13

In the manifest:

在清單:

android:screenOrientation="portrait"

android:configChanges="orientation|screenSize">

In your activity:

在你的活動:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

setContentView(R.layout.your_activity_layout);

#6

9

The following is the code which I used to display all activity in landscape mode:

下面是我用來在橫向模式下顯示所有活動的代碼:

android:configChanges="orientation|keyboardHidden"

android:name="abcActivty"/>

#7

5

A quick and simple solution is for the AndroidManifest.xml file, add the following for each activity that you wish to force to landscape mode:

一個快速而簡單的解決方案是AndroidManifest。xml文件,為您希望強制到橫向模式的每個活動添加以下內容:

android:screenOrientation="landscape"

#8

4

That's it!! Long waiting for this fix.

就是這樣! !我等這個補丁很久了。

I've an old Android issue about double-start an activity that required (programmatically) landscape mode: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

我有一個關於雙重啟動的Android問題,它要求(編程)橫向模式:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

Now Android make Landscape mode on start.

現在Android開始做橫向模式。

#9

3

Arslan, why do you want to force orientation pro grammatically, though there's already a way in manifest

Arslan,為什么要在語法上強制方向,盡管manifest

#10

1

Doing it in code is is IMO wrong and even more so if you put it into the onCreate. Do it in the manifest and the "system" knows the orientation from the startup of the app. And this type of meta or top level "guidance" SHOULD be in the manifest. If you want to prove it to yourself set a break in the Activity's onCreate. If you do it in code there it will be called twice : it starts up in Portrait mode then is switched to Landscape. This does not happen if you do it in the manifest.

在代碼中這樣做在我看來是錯誤的,如果你把它放到onCreate中就更錯了。在manifest中做,“系統”知道應用啟動時的方向,而這種元或頂層的“指導”應該在manifest中。如果你想證明給自己看,在活動的onCreate中設置一個斷點。如果你在代碼中這樣做,它會被調用兩次:它從豎屏模式開始,然后切換到風景。如果你在艙單中這樣做,這不會發生。

#11

1

This works for Xamarin.Android. In OnCreate()

這對於Xamarin.Android作品。在OnCreate()

RequestedOrientation = Android.Content.PM.ScreenOrientation.Landscape;

#12

0

For Android 4.0 (Ice Cream Sandwich) and later, I needed to add these, besides the landscape value.

對於Android 4.0(冰淇淋三明治)以及后來的版本,我需要添加這些,除了景觀價值。

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

Using only keyboardHidden|orientation would still result in memory leaks and recreation of my activities when pressing the power button.

只使用keyboardHidden|方向仍然會導致在按下電源按鈕時出現內存泄漏和活動恢復。

#13

0

Use the ActivityInfo (android.content.pm.ActivityInfo) in your onCreate method before calling setLayout method like this

在調用setLayout方法之前,在onCreate方法中使用ActivityInfo (android.content.pm.ActivityInfo)

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

#14

-23

Press CTRL+F11 to rotate the screen.

按CTRL+F11旋轉屏幕。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值