【问题标题】:Preventing my Activity to change orientation depending on its content防止我的活动根据其内容改变方向
【发布时间】:2018-07-30 08:54:43
【问题描述】:

在我的应用程序中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 魔术?提前致谢!

【问题讨论】:

标签: android android-activity orientation


【解决方案1】:

您可以在manifest.xml 中设置screenOrientation

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

此活动将始终保持portrait

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

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

编辑如果我理解正确,你想为不同的方向制作不同的布局。

然后创建一个layout-land目录并放置横向布局XML 该目录中的文件。

【讨论】:

  • 我已经知道这一点,但关键是,至少我的一个标签(它们是碎片,它们实际上没有方向)应该能够以横向显示。
  • 这也会出现,但首先,我的其他标签应该停止旋转。
  • 你能显示横向版本所需的输出 UI。
  • 不是现在,我想,还有太多的建筑面积,我会清理这个并为横向模式创建第二个布局。
  • 我仍然无法理解您的观点。让我知道您的问题是否已解决。
【解决方案2】:

要设置方向,只需在 manifest 中添加 screenOrientation activity tag

设置方向 - 纵向

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

设置方向 - 横向

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

或者您可以使用以下代码以编程方式执行此操作:-

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

【讨论】:

    【解决方案3】:

    只需将此行与您想在manifest.xml中以纵向模式使用的活动一起使用

     android:screenOrientation="portrait"
    

    喜欢这个

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

    【讨论】:

      【解决方案4】:

      Menifest 文件android:screenOrientation="portrait"activity 标记中添加此行。如下图,

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

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多