如何将安卓应用程序的方向锁定为手机中的纵向和平板电脑中的横向?
我正在开发一个 Android 应用程序,当用户旋转设备时,我不想将其方向更改为横向模式.此外,我希望锁定方向在手机上为纵向模式,在平板电脑上为横向模式.这可以实现吗,如果是,如何实现?谢谢.
I am developing an Android app whose orientation I don't want changed to landscape mode when the user rotates the device. Also, I want the locked orientation to be portrait mode on phones and landscape mode on tablets. Can this be achieved, if yes how? Thanks.
你只需要在 activity 元素.它会将您的方向限制为纵向.
You just have to define the property below inside the activity element in your AndroidManifest.xml
file. It will restrict your orientation to portrait.
android:screenOrientation="肖像"
android:screenOrientation="portrait"
示例:
<activity
android:name="com.example.demo_spinner.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
如果您希望这适用于整个应用程序,请在应用程序标签内定义以下属性,如下所示:
if you want this to apply to the whole app define the property below inside the application tag like so:
<application>
android:screenOrientation="sensorPortrait"
</application>
此外,根据下面 Eduard Luca 的评论,如果您想启用 180 度旋转,您还可以使用 screenOrientation="sensorPortrait"
.
Additionaly, as per Eduard Luca's comment below, you can also use screenOrientation="sensorPortrait"
if you want to enable rotation by 180 degrees.