我希望我的Activity只在纵向模式下运行,所以我尝试了不同的方法,但没有一种方法有效... 以下是尝试过的方法:
清单文件:
<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>
无效,即使我的手机屏幕旋转关闭了,仍然可以旋转。
接下来我尝试了从代码上解决。从清单中移除了添加的staff,并在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>
结果是:它不让我旋转,但当我这样做时,整个应用程序会冻结,直到我将其旋转回竖屏为止。
帮帮我!