【问题标题】:Run the application in Landscape mode only?仅在横向模式下运行应用程序?
【发布时间】:2011-11-23 10:26:13
【问题描述】:

我只想在我的设备上以横向模式运行 Hello World 示例应用程序。

如果设备更改为纵向,我想举起一台烤面包机。例如“请更改为横向模式以运行您的应用程序”。

我应该怎么做?

【问题讨论】:

    标签: android landscape


    【解决方案1】:

    您可以按以下方式以编程方式进行这两种操作:

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    

    或者在清单文件中你可以在这里给出

    <activity android:name=".YourActivity"  android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation"/>
    

    【讨论】:

      【解决方案2】:

      将此添加到清单文件中的活动标签中:

      android:screenOrientation="landscape"
      

      它将您的方向固定为横向模式,您无需向用户显示任何 toast。

      【讨论】:

        【解决方案3】:

        在您的清单文件(xml)中

        <activity android:name=".ActivityName" android:screenOrientation="landscape">
            </activity>
        

        也请参考Is that possible to check was onCreate called because of orientation change?

        【讨论】:

          【解决方案4】:

          你可以试试这样的

          Display display = getWindowManager().getDefaultDisplay(); 
          int width = display.getWidth();
          int height = display.getHeight();
          
          Log.v("log_tag", "display width is "+ width);
          Log.v("log_tag", "display height is "+ height);
          
          if(width<height){
              Toast.makeText(getApplicationContext(),"Device is in portrait mode",Toast.LENGTH_LONG ).show();
          } else {
              Toast.makeText(getApplicationContext(),"Device is in landscape  mode",Toast.LENGTH_LONG ).show();
          }
          

          【讨论】:

            【解决方案5】:

            您可以将configChanges-attribute 用于您想要捕捉屏幕方向变化的Activity。

            之后,你的Activity中的onConfigurationChanged()-method会在方向改变时被调用。

            要查看当前显示的方向,请查看这篇较早的帖子:Check orientation on Android phone

            之后,您可以炫耀您的信息或做任何您喜欢的事情(当然您也可以将其设置为仅横向显示)。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-01-17
              • 1970-01-01
              • 1970-01-01
              • 2013-05-18
              • 1970-01-01
              • 1970-01-01
              • 2018-03-21
              • 1970-01-01
              相关资源
              最近更新 更多