横竖屏布局小技巧
16lz
2021-12-04
android真机上自由切换横竖屏,在manifest文件activity标签里添加:
android:screenOrientation="sensor"
"sensor"
有物理的感应器来决定。如果用户旋转设备这屏幕会横竖屏切换。
屏幕切换会导致activity重新onCreate,
添加:android:configChanges="orientation" activity就不会onCreate了,可以在这个函数里配置管理,切换屏幕的数据操作
@Overridepublic void onConfigurationChanged(Configuration newConfig) {// TODO Auto-generated method stubsuper.onConfigurationChanged(newConfig);if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){}else if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){}Log.d(TAG, "onConfigurationChanged");}
如果要让软件在横竖屏之间切换,由于横竖屏的高宽会发生转换,有可能会要求不同的布局。可以通过以下方法来切换布局:
1)在res目录下建立layout-land和layout-port目录,相应的layout文件不变,比如main.xml。layout-land是横屏的layout,layout-port是竖屏的layout,其他的不用管,模拟器会自动寻找。
在编辑main.xml文件的时候可以选择横屏竖屏来调整布局,做到心中有谱
2)通过this.getResources().getConfiguration().orientation来判断当前是横屏还是竖屏然后来加载相应的xml布局文件。因为当屏幕变为横屏的时候,系统会重新呼叫当前Activity的OnCreate方法,你可以把以下方法放在你的 OnCreate中来检查当前的方向,然后可以让你的SetContentView来载入不同的Layout xml.
第二种方法就不用说了。
参考http://mingzaijun.iteye.com/blog/1261954
更多相关文章
- Android(安卓)Studio如何轻松整理字符串到string.xml中
- Android(安卓)NDK概述
- error: Error retrieving parent for item: No resource found t
- android 开发笔记
- Android(安卓)OTA 升级之二:脚本 ota_from_target_files
- Android高手进阶教程(四)之----Android(安卓)中自定义属性(attr.
- Android的多媒体框架OpenCore(PacketVideo)介绍
- Activity切换动画无效(android:windowIsTranslucent)影响(androi
- android使用http协议上传文件