繁体   English   中英

平板电脑Android中的横向和纵向模式

[英]Landscape and portrait mode in tablet Android

我有一个问题。我想在横向和纵向模式下使用该应用程序,但是我需要有关将设备纵向旋转到横向时的信息,该应用程序分为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。

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

@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分开的情况下,应使用gradle以及其各自的布局来运输每个apk。

您可以检查角度并执行任何操作。

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();
    }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM