简体   繁体   English

在Android中以编程方式使用人像和风景模式

[英]Working with portrait and landscape mode programmatically in android

I want to create the layouts for both portrait and landscape mode (in java) and change the orientations programmatically in java file. 我想为纵向和横向模式(在Java中)创建布局,并以编程方式在Java文件中更改方向。 Can anybody help me? 有谁能够帮助我?

you can programmatically force a change in orientation using the setRequestOrientation() method of the Activity class. 您可以使用Activity类的setRequestOrientation()方法以编程方式强制更改方向。

 public class Example extends Activity 
  {
  @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //---change to landscape mode---
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
   }
 }

To change to portrait mode, use the ActivityInfo.SCREEN_ORIENTATION_PORTRAIT constant: 要更改为纵向模式,请使用ActivityInfo.SCREEN_ORIENTATION_PORTRAIT常量:

For more information, refer this . 有关更多信息,请参阅

EDIT 编辑

you can add orientation to your specific Activity in AndroidManifest.xml like this: 您可以在AndroidManifest.xml向特定的Activity添加方向,如下所示:

 <activity android:name=".DemoActivity"
  android:label="@string/app_name"
  android:screenOrientation="portrait">

As far as Layouts are concerned, it is best advised to create different folders under res/ . 就布局而言,最好建议在res/下创建不同的文件夹。 For example, you can create res/layout-port for portrait and res/layout-land for landscape. 例如,您可以为纵向创建res/layout-port ,为风景创建res/layout-land

Try this: 尝试这个:

if(context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) 
    {
        // code to do for Landscape Mode     
    } else {

        // code to do for Portrait Mode  
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 纵向模式下的Android Landscape VideoView - Android Landscape VideoView in portrait mode 适用于Android应用程序的横向和纵向模式 - Landscape and portrait mode for android application 平板电脑Android中的横向和纵向模式 - Landscape and portrait mode in tablet Android Android 中的横向模式更改为纵向 - Landscape Mode change to portrait in Android 将人像旋转到风景时,我要关闭力量,我尝试了android:configChanges =“ keyboard | keyboardHidden”并以人像模式工作吗? - Getting Force Close when rotating portrait to landscape and I tried android:configChanges=“keyboard|keyboardHidden” and working in portrait mode? 如何在android中以编程方式控制横向和纵向? - How to control landscape and portrait programmatically in android? Android相机为横向,但以纵向模式使用 - Android camera is in landscape but used in portrait mode Android中纵向和横向模式的不同设计 - different design for portrait and landscape mode in android zxing相机肖像模式和Android上的风景 - zxing camera portrait mode and landscape on android 修复了Android中横向和纵向模式下的页脚 - Fixed footer in landscape and portrait mode in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM