防止我的 Activity 根据其内容改变方向

Preventing my Activity to change orientation depending on its content

在我的应用程序 MainActivity 中包含一个 BottomNavigation,因此它可以显示不同的选项卡。现在我的意图是,强制他们中的一些人保持纵向,无论用户如何握住设备。

MainActivity.java:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
    bottomNav.setOnNavigationItemSelectedListener(navListener);
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new OverviewFragment()).commit();
}

private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;

                switch (item.getItemId()) {
                    case R.id.nav_overview:
                        selectedFragment = new OverviewFragment();
                        break;
                    case R.id.nav_reports:
                        selectedFragment = new ReportsFragment();
                        break;
                    case R.id.nav_settings:
                        selectedFragment = new SettingsFragment();
                        break;
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
                return true;
            }
        };

}

activity_main.xml

<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/bottom_navigation"/>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="?android:attr/windowBackground"
    app:menu="@menu/bottom_navigation"/>

至少应有一个选项卡可以纵向和横向显示。我是否必须向 xml 添加一些东西,或者做一些 java 魔术?提前致谢!

您可以在manifest.xml中设置screenOrientation

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

这个 activity 将一直保持 portrait

如果您以编程方式需要它,那么您可以使用它。

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

编辑如果我没理解错的话,你想为不同的方向做不同的布局。

Then create a layout-land directory and place the landscape layout XML file in that directory.

要设置方向,只需在 activity tag

中的 manifest 中添加 screenOrientation

to set orientation - portrait

  <activity
        android:name=".YourActivityNAME"
        android:theme="@style/AppTheme.NoActionBar"
        android:screenOrientation="portrait"/>

to set orientation - landscape

  <activity
        android:name=".YourActivityNAME"
        android:theme="@style/AppTheme.NoActionBar"
        android:screenOrientation="landscape"/>

Or you can do this Programmatically by using code below:-

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

只需将此行与您想在 manifest.xml

中以纵向模式使用的 activity 一起使用
 android:screenOrientation="portrait"

像这样

<activity
            android:name=".app.comman.MainActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateAlwaysHidden" />

将此行添加到 Menifest 文件 android:screenOrientation="portrait" 中的 activity 标记中。如下所示,

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

将这些行放在清单中,以便屏幕方向为 纵向

 <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/MyMaterialTheme" />