android黄油刀怎么解决ios横竖屏解决方案布局问题

博客访问: 372222
博文数量: 27
博客积分: 509
博客等级: 下士
技术积分: 809
注册时间:
APP发帖 享双倍积分
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: 嵌入式
应用程序在设计的时候要考虑是否进行横竖屏的切换,如何切换的问题。如果不进行设置,则默认是可以记性横竖屏切换的,这样在实际使用过程中可以出现意想不到的结果。所以应该注意这个问题。
·与横竖屏切换相关的两个目录
可以在res目录下建立layout-port和layout-land两个目录,里面分别放置竖屏和横屏两种布局文件,这样在手机屏幕方向变化的时候系统会自动调用相应的布局文件,避免一种布局文件无法满足两种屏幕显示的问题。
·与横竖屏切换相关的两个属性
android:screenOrientation表示activity方向的属性。值有:
·landscape&&& 横向
·portrait&&&&&&& 纵向
·sensor&&&&&& 重力感应
·nosensor&&&&&&& 无重力感应
·unspecified&&&& 默认值 由系统来判断显示方向.判定的策略是和设备相关的,所以不同的设备会有不同的显示方向.
·user&&&&&&&& 用户当前首选的方向
·behind&&&&&& 和该Activity下面的那个Activity的方向一致(在Activity堆栈中的)
如果想强制应用程序不进行屏幕切换,可以为这个属性赋landscape或者portrait属性
在AndroidManifiest.xml文件中使用android : configChanges = " keyboardHidden | orientation "这个属性,当我们横竖屏切换的时候会直接调用onConfigurationChanged方法,而不会重新执行onCreate方法。所以设置了上面的属性之后,可以在程序中重载这个函数
& @Override
 public void onConfigurationChanged(Configuration newConfig) {
  &&&&&&& super.onConfigurationChanged(newConfig);
//if(this.getResources().getConfiguration().orientation== Configuration.ORIENTATION_LANDSCAPE)
  &&&&&&& if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
  &&&&&&&&&&& tv.setText("横屏");
&&&&& & setContentView(R.layout.file_list_landscape);
  &&&&&&& }else{
  &&&&&&&&&&& tv.setText("竖屏");
&&&&& & setContentView(R.layout.file_list);
  &&&&&&& }
Android3.2API level 13
Androidconfigchanges = “orientation|screenSize”
·程序实现横竖屏的切换处理
androidActivityActivity,在ActivityCreate
Activity ActivityonSaveInstanceState()ActivityBundleActivity onCreate(Bundle)onRestoreInstanceState(Bundle)BundleBundle
/**Bundle**/&&
& & @Override&&& & protected void onSaveInstanceState(Bundle outState) {&&
& & long outTime = System.currentTimeMillis();&&& & //Bundle&&
& & outState.putLong("time", outTime);&&& & Log.v("InstanceState", "outTime is "+ outTime);&&
& && &&&super.onSaveInstanceState(outState);&&& & }&&
& && && & /**Bundle**/&&
& & @Override&&& & protected void onRestoreInstanceState(Bundle savedInstanceState) {&&
& & //&&& & Long saveTime = savedInstanceState.getLong("time");&&
& && && & Log.v("InstanceState", "saveTime is "+ saveTime);&&
& && &&&super.onRestoreInstanceState(savedInstanceState);&&& & }&&
阅读(5526) | 评论(0) | 转发(1) |
相关热门文章
给主人留下些什么吧!~~
请登录后评论。&&&&&&&&&&&
在以前的版本中只要在AndroidManifest.xml文件中对activity指定android:configChanges="keyboardHidden|orientation"属性,转屏的时候就会不再重新调用OnCreate()函数,而是调用onConfigurationChanged()。
但是在自从android3.2以后,再这样设置的话,会发现转屏后仍然会调用OnCreate(),而不是onConfigurationChanged();跟踪framework层代码,就会发现问题所在,是由于google在android3.2中添加了screensize改变的通知,在转屏的时候,不仅是orientation发生了改变,screensize同样也发生了改变,而在判断是调用onConfigurationChanged还是OnCreate时,采用的是如下判断:
int&diff = activity.mCurrentConfig.diff(config);
if&(diff != 0) {&&&&&&&&&&&&&&&&
// If this activity doesn't handle any of the config changes then don't bother calling onConfigurationChanged as we'regoing to destroy it.
if&((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
shouldChangeConfig =
public&int getRealConfigChanged() {
return&applicationInfo.targetSdkVersion & android.os.Build.VERSION_CODES.HONEYCOMB_MR2 ? (configChanges | ActivityInfo.CONFIG_SCREEN_SIZE
| ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) : configC
通过上面的分析,可发现有两种方法解决该问题:(只需要修改AndroidManifest.xml)
1.指定android:configChanges="keyboardHidden|orientation|screenSize",其他的代码和以前的代码一样处理;
2.在AndroidManifest.xml中指定targetSdkVersion为3.2以前的版本(3.2的版本号为13),系统会自动加上screenSize属性值。
&&&比如:&uses-sdk android:minSdkVersion="3" android:targetSdkVersion="12" /&&&
阅读(...) 评论() &

我要回帖

更多关于 android 横竖布局 的文章

 

随机推荐