当前位置:首页 » 安卓系统 » android判断横屏

android判断横屏

发布时间: 2025-10-12 17:43:38

㈠ android如何在代码中判断横竖屏

Android中判断横竖屏是通过Configuration 这个类来判断的。

  1. Configuration.ORIENTATION_LANDSCAPE 表示横屏。

  2. Configuration.ORIENTATION_PORTRAIT表示竖屏。


以下是完整代码:

public boolean isScreenChange() {

Configuration mConfiguration = context.getResources().getConfiguration(); //获取设置的配置信息
int ori = mConfiguration.orientation ; //获取屏幕方向
if(ori == Configuration.ORIENTATION_LANDSCAPE){
//横屏
return true;
}else if(ori == Configuration.ORIENTATION_PORTRAIT){
//竖屏
return false;
}
return false;
}


避免在转屏时重启Activity

android中每次屏幕方向切换时都会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次 Create的时候载入配置,那样,进行中的游戏就不会自动重启了!
要避免在转屏时重启Activity,可以通过在AndroidManifest.xml文件中重新定义方向(给每个Activity加上android:configChanges=”keyboardHidden|orientation”属性)。
在需要控制屏幕显示方向的Activity中重写onConfigurationChanged(Configuration newConfig)方法,这样在转屏时就不会重启Activity了。

㈡ android 不是activity怎么监测屏幕旋转

Camera相关的程序,被屏幕旋转搞得头大,一方面得考虑屏幕旋转后布局的变化,另一方面得搞清楚屏幕的旋转方向、角度与Camera的Preview角度的关系。本来通过重载Activity的onConfigurationChanged方法,可以检测到屏幕旋转,但发现有一个问题,它只能检测水平方向与垂直方向的切换,无法检测180度的跳转(例如:水平方向突然转180度到水平方向),所以最后不得不换成OrientationEventListener方法来解决问题。在这里分享下经验,并就此顺便总结下Android开发中屏幕旋转的处理吧。

1. 不做任何处理的情况下

如果没有针对性地做任何处理的话,默认情况下,当用户手机的重力感应器打开后,旋转屏幕方向,会导致app的当前activity发生onDestroy-> onCreate,会重新构造当前activity和界面布局,很多横屏/竖屏的布局如果没有很好的设计的话,转换为竖屏/横屏后,会显示地很难看。

如果想很好地支持屏幕旋转,则建议在res中建立layout-land和layout-port两个文件夹,把横屏和竖屏的布局文件放入对应的layout文件夹中。

2. 如何设置固定的屏幕方向

在AndroidManifest.xml对应的 activity 属性中,添加:

android:screenOrientation="landscape" //横屏
android:screenOrientation="portrait" //竖屏

那么,默认的情况下,应用启动后,会固定为指定的屏幕方向,即使屏幕旋转,Activity也不会出现销毁或者转向等任何反应。

3. 强制开启屏幕旋转效果

如果用户的手机没有开启重力感应器或者在AndroidManifest.xml中设置了android:screenOrientation,默认情况下,该Activity不会响应屏幕旋转事件。如果在这种情况下,依然希望Activity能响应屏幕旋转,则添加如下代码:

// activity的 onCreate 函数中
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

4. 屏幕旋转时,不希望activity被销毁

如果希望捕获屏幕旋转事件,并且不希望activity 被销毁,方法如下:

(1)在AndroidManifest.xml对应的activity属性中,添加:

android:configChanges="orientation|screenSize"

(2)在对应的activity中,重载函数onConfigurationChanged

@Override
public voidonConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}

在该函数中可以通过两种方法检测当前的屏幕状态:

第一种:

判断newConfig是否等于Configuration.ORIENTATION_LANDSCAPE,Configuration.ORIENTATION_PORTRAIT

当然,这种方法只能判断屏幕是否为横屏,或者竖屏,不能获取具体的旋转角度。

第二种:

调用this.getWindowManager().getDefaultDisplay().getRotation();

该函数的返回值,有如下四种:

Surface.ROTATION_0,Surface.ROTATION_90,Surface.ROTATION_180,Surface.ROTATION_270

其中,Surface.ROTATION_0 表示的是手机竖屏方向向上,后面几个以此为基准依次以顺时针90度递增。

(3) 这种方法的Bug

最近发现这种方法有一个Bug,它只能一次旋转90度,如果你突然一下子旋转180度,onConfigurationChanged函数不会被调用。

㈢ android怎么设置横竖屏切换

方法一:

1、首先在android手机打开“设置”这个选项的,点击“显示”这个功能;

㈣ android 怎么设置锁屏界面可以横竖屏切换

Android横竖屏要解决的问题应该就两个:
一.布局问题
二.重新载入问题

1.布局问题:如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的AndroidManifest.xml中找到你所指定的activity中加上android:screenOrientation属性,他有以下几个参数:
"unspecified":默认值 由系统来判断显示方向.判定的策略是和设备相关的,所以不同的设备会有不同的显示方向.
"landscape":横屏显示(宽比高要长)
"portrait":竖屏显示(高比宽要长)
"user":用户当前首选的方向
"behind":和该Activity下面的那个Activity的方向一致(在Activity堆栈中的)
"sensor":有物理的感应器来决定。如果用户旋转设备这屏幕会横竖屏切换。
"nosensor":忽略物理感应器,这样就不会随着用户旋转设备而更改了("unspecified"设置除外)。
也可以在Java代码中通过setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)来设置。
如果要让软件在横竖屏之间切换,由于横竖屏的高宽会发生转换,有可能会要求不同的布局。可以通过以下方法来切换布局:
1)在res目录下建立layout-land和layout-port目录,相应的layout文件不变,比如main.xml。layout-land是横屏的layout,layout-port是竖屏的layout,其他的不用管,模拟器会自动寻找。
2)通过 this.getResources().getConfiguration().orientation来判断当前是横屏还是竖屏然后来加载相应的 xml布局文件。因为当屏幕变为横屏的时候,系统会重新呼叫当前Activity的onCreate方法,你可以把以下方法放在你的onCreate中来检查当前的方向,然后可以让你的setContentView来载入不同的layout xml.
1 if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
2 Log.i("info","landscape"); // 横屏
3 }
4 else if(this.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {
5 Log.i("info","portrait"); // 竖屏
6 }
在onConfigurationChanged()方法中也可以检测拥有硬键盘的键盘状态
1 //检测实体键盘的状态:推出或者合上
2 if (newConfig.hardKeyboardHidden ==Configuration.HARDKEYBOARDHIDDEN_NO){
3 //实体键盘处于推出状态,在此处添加额外的处理代码
4 }
5 else if(newConfig.hardKeyboardHidden ==Configuration.HARDKEYBOARDHIDDEN_YES){
6 //实体键盘处于合上状态,在此处添加额外的处理代码
7 }
2.重新载入问题。如果不需要从新载入,可以在AndroidManifest.xml中加入配置 android:configChanges="orientation|keyboardHidden",配置 android:configChanges的作用就是如文档所说的:Specify one or more configuration changesthat the activity will handle itself. If not specified, the activity will berestarted if any of these configuration changes happen in the system。这样在程序中Activity就不会重复的调用onCreate()甚至不会调用onPause、onResume.只会调用一个 onConfigurationChanged(Configuration newConfig)。如果需要重新载入,则不需要做任何修改。不过如果需要在重新载入过程中保存之前的操作内容或数据,则需要保存之前的数据。然后在 activity的onCreate()中取出来。当然,如此就不能设置android:configChanges()了,否则就不会调用 onCreate()方法。
如果要彻底禁止翻转,可以设置android:screenOrientation的属性为nosensor,如此就可以忽略重力感应带来的麻烦了。不过在模拟器上不管用,在真机上是正确的。android:screenOrientation="portrait"
则无论手机如何变动,拥有这个属性的activity都将是竖屏显示。
android:screenOrientation="landscape",为横屏显示。
这里提一个小知识,Android模拟器中,快捷键"Ctrl+F11/F12"可以实现转屏

㈤ android开发横竖屏问题

Android横屏竖屏设置

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置成全屏模式

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE););//强制为横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
我做的东西里面还用到了去掉标题栏。
我也贴出来
requestWindowFeature(Window.FEATURE_NO_TITLE);

垂直居中:

android:layout_centerVertical="true"

水平居中:

android:layout_centerHorizontal="true"

1.hideStatusbarAndTitlebar()隐藏statusbar和titlebar.

private void hideStatusbarAndTitlebar() {
final Window win = getWindow();
// No Statusbar
win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// No Titlebar
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
2.设置屏幕显示模式ScreenOrientation.

在activity里设置android:screenOrientation的值。
android:screenOrientation的属性有以下值:
unspecified(默 认值,由系统判断状态自动切换),The default value. The system chooses the orientation. The policy it uses, and therefore the choices made in specific contexts, may differ from device to device.
landscape,横屏
portrait,竖屏
user(用户当前设置的orientation值),The user's current preferred orientation.
behind(下一个要显示的Activity的orientation值),The same orientation as the activity that's immediately beneath it in the activity stack.
sensor(传 感器的方向),The orientation determined by a physical orientation sensor. The orientation of the display depends on how the user is holding the device; it changes when the user rotates the device.
nosensor(不 使用传感器,这个效果差不多等于unspecified).An orientation determined without reference to a physical orientation sensor. The sensor is ignored, so the display will not rotate based on how the user moves the device. Except for this distinction, the system chooses the orientation using the same policy as for the "unspecified" setting.

3.水平/垂直居中的方法.

设置parent的android:gravity为"center"。

4.获得当前屏幕宽高的方法.

Display display = getWindowManager().getDefaultDisplay();
Config.screenWidth = display.getWidth();
Config.screenHeight = display.getHeight();

热点内容
华意压缩机股份 发布:2025-10-12 20:38:11 浏览:927
逍遥安卓怎么样 发布:2025-10-12 20:36:06 浏览:291
怎么设置电脑进入密码怎么设置密码 发布:2025-10-12 20:34:36 浏览:525
文件存储时怎么会显示多个文件 发布:2025-10-12 20:33:55 浏览:957
外形编程代码 发布:2025-10-12 20:32:39 浏览:90
夕颜源码 发布:2025-10-12 20:18:44 浏览:735
立体存储架 发布:2025-10-12 20:15:02 浏览:606
如何在pc端运行安卓文件在哪里 发布:2025-10-12 20:14:11 浏览:288
app打包网站源码 发布:2025-10-12 20:05:28 浏览:708
可编程设计师 发布:2025-10-12 19:55:22 浏览:114