android开发横竖屏问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android开发横竖屏问题相关的知识,希望对你有一定的参考价值。

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();
参考技术A 在eclipse 里面run dialog->target 里面可以设置成自动横屏追问

怎么设置,里面是英文,能麻烦详细点啊

参考技术B 模拟器里面的自动横屏打钩试试追问

在哪里设置?

追答

。。。安卓手机都有的。。。如果找不到可以先把语言设置成中文,然后进设置------》显示,就可以找到了

Android 横竖屏切换

1.横竖屏切换

Android横竖屏要注意的问题:

①布局问题

②重新载入问题

③生命周期问题

 

2.布局问题

①如果不想让app在横竖屏之间切换,可以在AndroidManifest.xml中指定的activity中加上android:screenOrientation属性,有以下几个参数:

"unspecified":默认值 由系统来判断显示方向。判定的策略是和设备相关的,所以不同的设备会有不同的显示方向.。

"landscape":横屏显示

"portrait":竖屏显示

"user":用户当前首选的方向 

"behind":和该Activity下面的那个Activity的方向一致(在Activity堆栈中的) 

"sensor":由物理的感应器来决定。如果用户旋转设备这屏幕会横竖屏切换。 

"nosensor":忽略物理感应器,这样就不会随着用户旋转设备而更改了("unspecified"设置除外)。

也可以在Java代码中通过setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)来设置。

②如果要让app在横竖屏之间切换,由于横竖屏的高宽会发生转换,有可能会要求不同的布局。可以通过以下方法来切换布局:

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。

if (this.getResources().getConfiguration( ).orientation == Configuration.ORIENTATION_LANDSCAPE)

    Log.i("info","landscape"); // 横屏

else if(this.getResources().getConfiguration( ).orientation ==Configuration.ORIENTATION_PORTRAIT)

    Log.i("info","portrait"); // 竖屏

3.重新载入问题

①如果不需要重新载入,可以在AndroidManifest.xml中加入配置 android:configChanges="orientation|keyboardHidden"。配置configChanges的作用就是如文档所说的:Specify one or more configuration changes that 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()方法。

 

3.生命周期问题

测试横竖屏切换时activity的生命周期:

①新建一个Activity,把各个生命周期打印出来,运行Activity,得到如下信息

onCreate-->

onStart-->

onResume-->

②按切换成横屏时

onSaveInstanceState-->

onPause-->

onStop-->

onDestroy-->

onCreate-->

onStart-->

onRestoreInstanceState-->

onResume-->

③再切换成竖屏时,发现打印了两次相同的log

onSaveInstanceState-->

onPause-->

onStop-->

onDestroy-->

onCreate-->

onStart-->

onRestoreInstanceState-->

onResume-->

onSaveInstanceState-->

onPause-->

onStop-->

onDestroy-->

onCreate-->

onStart-->

onRestoreInstanceState-->

onResume-->

④修改AndroidManifest.xml,把该Activity添加 android:configChanges="orientation",执行切换到横屏

onSaveInstanceState-->

onPause-->

onStop-->

onDestroy-->

onCreate-->

onStart-->

onRestoreInstanceState-->

onResume-->

⑤再执行切换到竖屏,发现不会再打印相同信息,但多打印了一行onConfigChanged

onSaveInstanceState-->

onPause-->

onStop-->

onDestroy-->

onCreate-->

onStart-->

onRestoreInstanceState-->

onResume-->

onConfigurationChanged-->

⑥把android:configChanges="orientation" 改成 android:configChanges="orientation|keyboardHidden",执行切换到横屏,就只打印onConfigChanged

onConfigurationChanged-->

⑦执行切换到竖屏

onConfigurationChanged-->

onConfigurationChanged-->

所以,可以得到结论:

①不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次。

②设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次。

③设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法。

 

 

以上是关于android开发横竖屏问题的主要内容,如果未能解决你的问题,请参考以下文章

Android 横竖屏切换

软件的横竖屏切换

android自定义控件 切换横竖屏报错

Android视频播放和横竖屏切换

Android横竖屏切换View设置不同尺寸或等比例缩放的XML解决方案

Android 横竖屏切换