【问题标题】:Landscape and portrait mode in tablet Android平板电脑 Android 中的横向和纵向模式
【发布时间】:2016-04-11 11:55:49
【问题描述】:

我有一个问题。我想在横向和纵向模式下使用该应用程序,但我需要有关当我将设备纵向旋转为横向时,应用程序拆分为 2 个片段的信息。

我研究了许多网站,但没有必要的信息。我该怎么做?

编辑

 productFlavors {
        phone {
            applicationId "packageName.app.phone"
            buildConfigField 'boolean', 'IsPhone', 'true'
            versionName ""
        }
        tablet {
            applicationId "packageName.app.tablet"
            buildConfigField 'boolean', 'IsPhone', 'false'
            versionName ""
        }
    }

我拆分了我的 apk。我有一个 phone.apk 和 tablet.apk。

【问题讨论】:

  • 您可以在 layout_land 文件夹中指定横向模式。
  • 是的,我知道这一点,但我用 gradle 拆分了我的 apk。我有一个 phone.apk 和一个 tablet.apk。当我创建一个“layout_land”文件夹时,对我没有用。
  • 你应该看到这个文档developer.android.com/intl/pt-br/guide/practices/…关于支持平板电脑和手机

标签: android rotation tablet


【解决方案1】:

如果您真的想知道设备何时旋转,您可以尝试这样的操作。

@Override
public void onConfigurationChanged(Configuration config) {
    super.onConfigurationChanged(config);

    if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // "landscape"
    } else if (config.orientation == Configuration.ORIENTATION_PORTRAIT){
        //"portrait"
    }
}

但通常你不应该这样做。您只需为portrait 提供一种布局,为lanscape 提供另一种布局,然后让布局加载所需的片段。

在分开 phone.apk 和 tablet.apk 的情况下,每个 apk 都应使用 gradle 及其各自的布局集进行交付。

【讨论】:

    【解决方案2】:

    你可以检查它的角度并做任何事情。

    import android.app.Activity;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.OrientationEventListener;
    import android.widget.Toast;
    
    public class AndroidOrientationSensor extends Activity {
    
        OrientationEventListener myOrientationEventListener;
    
        /**
         * Called when the activity is first created.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_main);
    
            myOrientationEventListener
                    = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
    
                @Override
                public void onOrientationChanged(int arg0) {
                    // TODO Auto-generated method stub
                    Log.d("GORIO", "angle: " + String.valueOf(arg0));
                }
            };
    
            if (myOrientationEventListener.canDetectOrientation()) {
                Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show();
                myOrientationEventListener.enable();
            } else {
                Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show();
                finish();
            }
        }
    
        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            myOrientationEventListener.disable();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 2012-03-26
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 2017-12-11
      相关资源
      最近更新 更多