将屏幕方向锁定到其(自然)默认方向
我想将屏幕方向锁定为默认方向。我在实现这个方面遇到问题。最初我从屏幕锁定屏幕到肖像。它适用于肖像默认设备。但是很多平板电脑都将横向设置为默认设置,因此在这些设备上锁定到纵向不适合,我想检测这个默认方向并将其锁定。我的意思是,如果风景是默认的方向,我想锁定方向到风景,如果它的肖像然后锁定到端口。这个怎么做。我被困在这部分。我不想支持这两种方向(自动)。请帮忙将屏幕方向锁定到其(自然)默认方向
谢谢。
对于不同的设备有一个默认方向,例如galaxy 10平板电脑的默认方向与nexus 7平板电脑不同。当你将得到以下数值显示的方向:
这么说,你有你的锁定方法做的是以下几点:
public void mLockScreenRotation(FA_Padre actividad){
int buildVersionSDK = Build.VERSION.SDK_INT;
Display display = ((WindowManager) actividad.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int orientation=0;
if(buildVersionSDK >= 8){
orientation=display.getRotation();
}
/****************Phone*************************************/
if(buildVersionSDK < 8){// older Android versions with only two orientations
switch (actividad.getResources().getConfiguration().orientation){
case Configuration.ORIENTATION_PORTRAIT:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Configuration.ORIENTATION_LANDSCAPE:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
}else if((buildVersionSDK > 7) && (!GlobalInfo.isTablet())){// Newer Android phones with more than two orientations
switch(orientation){
case Surface.ROTATION_0:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_270:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
}
/****************TABLET*************************************/
}else{
int width = 0;
int height = 0;
switch(orientation){
/*If the default orientation of the device is landscape Rotation_0 and rotation_180 will be the case if the device is being held in landscape. if the default orientation of the device is portrait rotation_0 or 180 will only be the case if the device is in portrait mode*/
case Surface.ROTATION_0:
case Surface.ROTATION_180:
width = display.getWidth();
height = display.getHeight();
break;
/*the opposite in here*/
case Surface.ROTATION_90: //
case Surface.ROTATION_270:
width = display.getHeight();
height = display.getWidth();
break;
default:
break;
}
if(width > height){//Default ORIENTATION = LANDSCAPE, lock the screen in the current orientation
switch(orientation){
case Surface.ROTATION_0:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_90:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_180:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_270:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
}
} else {//Default ORIENTATION = PORTRAIT, lock the screen in the current orientation
switch(orientation){
case Surface.ROTATION_0:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_270:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
}
}
}
}
试试这个锁屏默认
Lockorientationactivity:
public class Lockorientationactivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int getConf=this.getResources().getConfiguration().orientation;
if(getConf==Configuration.ORIENTATION_PORTRAIT)
{
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Toast.makeText(getBaseContext(), "ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show();
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
setContentView(R.layout.main);
}
}
但你必须设置android:configChanges="orientation"
的活动。
好吧,这是它如何为我工作。我不认为它是一个非常合适的解决方案。我刚才把这个放在我的清单android:screenOrientation =“nosensor” 现在景观设备正在采取景观特定的布局和肖像设备正在采取肖像特定的布局。由于我已经给出了“nosensor”,所以Plus方向被锁定。但我读了一些文章,这仍然是不正确的方式,因为它可能无法像打开软键盘时有效。我现在就去找它,直到我找到其他方法。 谢谢大家。 – 2012-03-10 05:55:45
要锁定屏幕通过代码,你必须使用屏幕的实际旋转角度(0,90,180,270),你必须知道它的自然位置,在智能手机中,自然位置将是肖像,在平板电脑中,它将是风景。
下面是代码(锁定和解锁方法),它已在某些设备(智能手机和平板电脑)中测试过,并且效果很好。
public static void lockScreenOrientation(Activity activity)
{
WindowManager windowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
Configuration configuration = activity.getResources().getConfiguration();
int rotation = windowManager.getDefaultDisplay().getRotation();
// Search for the natural position of the device
if(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE &&
(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) ||
configuration.orientation == Configuration.ORIENTATION_PORTRAIT &&
(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270))
{
// Natural position is Landscape
switch (rotation)
{
case Surface.ROTATION_0:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_90:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_180:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_270:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
}
}
else
{
// Natural position is Portrait
switch (rotation)
{
case Surface.ROTATION_0:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_270:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
}
}
}
public static void unlockScreenOrientation(Activity activity)
{
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
它不适合我。只需在onCreate中复制并粘贴并调用lockScreenOrientation。 – ARLabs 2014-09-18 08:54:57
...没有平板作为默认的风景。没有“默认”。当我正在做一些富有成效的事情时,我的肖像通常是肖像,而当我正在娱乐时,我的肖像是在风景中。你为什么不想尊重用户的偏好? – 2012-03-10 04:27:17
http://android-developers.blogspot.in/2010/09/one-screen-turn-deserves-another.html“不要以为肖像是默认模式。”许多片剂都是风景自然的。 – 2012-03-10 04:29:47
我读过。仍然没有“默认”(名词)。有连接了键盘的变压器的人肯定会默认(动词)到风景。断开键盘,它取决于用户,用户正在做什么,月亮的相位等等。为什么你不想尊重用户的偏好? – 2012-03-10 05:03:14