简体   繁体   English

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

[英]Landscape and portrait mode in tablet Android

I have a question.I wanna use the app in landscape and portrait mode but I need information about when I rotate the device portrait to landscape, the app split to 2 fragments. 我有一个问题。我想在横向和纵向模式下使用该应用程序,但是我需要有关将设备纵向旋转到横向时的信息,该应用程序分为2个片段。

I research many websites but I haven't necessary infos about that. 我研究了许多网站,但没有必要的信息。 How can I do that? 我怎样才能做到这一点?

EDIT 编辑

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

I split my apk.I have a phone.apk and tablet.apk. 我拆分了我的apk,我有一个phone.apk和tablet.apk。

If you really want to know when the device rotate you can try something like this. 如果您真的想知道设备何时旋转,可以尝试执行以下操作。

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

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

But usually you are not supposed to do this. 但是通常不应该这样做。 You just need to provide one layout for portrait and another one for lanscape and then let the layout load the required Fragments. 您只需为portrait提供一种布局,为lanscape提供另一种布局,然后让该布局加载所需的片段。

In the case of to separate phone.apk and a tablet.apk each apk should be shipped, using gradle, with its respective set of layout. 在将phone.apk和tablet.apk分开的情况下,应使用gradle以及其各自的布局来运输每个apk。

You could check angle of it and do anything. 您可以检查角度并执行任何操作。

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