我希望我的 Activity 只在纵向模式下运行,所以我尝试了不同的方法,但都没有用......它们在这里:
list :
<application
android:name=".App"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="intent.to.front" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
没有用,仍然可以旋转,即使我的手机关闭了屏幕旋转。
接下来我从代码中尝试。从 list 中删除添加的人员,并将其写入 mainactivity 类:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Utilities.setLocale(getApplicationContext());
setContentView(R.layout.activity_main);
这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<include layout="@layout/app_bar_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
app:itemIconTint="@color/grey"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
结果是:它不允许我旋转,但当我旋转时,整个应用程序会卡住并且无法执行任何操作,直到我将其旋转回垂直方向。
帮帮我!
最佳答案
添加 android:screenOrientation="portrait"
可以简单地达到目的:
<activity android:name=".YourActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"/>
关于安卓屏幕方向 : Portrait doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34450708/