平板电脑中的横向和纵向模式 Android
Landscape and portrait mode in tablet Android
我有一个 question.I 想在横向和纵向模式下使用该应用程序,但我需要有关何时将设备纵向旋转为横向、该应用程序拆分为 2 个片段的信息。
我研究了很多网站,但没有必要的信息。我该怎么做?
编辑
productFlavors {
phone {
applicationId "packageName.app.phone"
buildConfigField 'boolean', 'IsPhone', 'true'
versionName ""
}
tablet {
applicationId "packageName.app.tablet"
buildConfigField 'boolean', 'IsPhone', 'false'
versionName ""
}
}
我拆分了我的 apk.I 有一个 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
提供另一种布局,然后让布局加载所需的 Fragments。
在将 phone.apk 和 tablet.apk 分开的情况下,每个 apk 都应使用 gradle 及其各自的布局集进行发布。
你可以检查它的角度并做任何事情。
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();
}
}
我有一个 question.I 想在横向和纵向模式下使用该应用程序,但我需要有关何时将设备纵向旋转为横向、该应用程序拆分为 2 个片段的信息。
我研究了很多网站,但没有必要的信息。我该怎么做?
编辑
productFlavors {
phone {
applicationId "packageName.app.phone"
buildConfigField 'boolean', 'IsPhone', 'true'
versionName ""
}
tablet {
applicationId "packageName.app.tablet"
buildConfigField 'boolean', 'IsPhone', 'false'
versionName ""
}
}
我拆分了我的 apk.I 有一个 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
提供另一种布局,然后让布局加载所需的 Fragments。
在将 phone.apk 和 tablet.apk 分开的情况下,每个 apk 都应使用 gradle 及其各自的布局集进行发布。
你可以检查它的角度并做任何事情。
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();
}
}