Android杂谈--layout的横竖屏处理

横竖屏处理是开发应用是比较基础的一个要点,几乎都会用到。网上有一大堆的横竖屏切换的文章,但是翻了n页以后发现竟然清一色的是转载,所以不想浪费时间到这个上面,还是自己根据自己的需求与体会总结一下吧,也方便以后查阅

一、layout-land和layout-prot的区别与使用

默认情况下,创建的Android项目里只有一个layout文件夹,尽管这样也可以横竖屏切换用,但是某些布局横屏过后闲的格外的丑,如下图




 



横屏过后就显示的不全了,有时候看着比较纠结。所以需要在横屏的使用重新载入新的布局文件

解决办法是:先把layout目录删除了,因为可能跟之后的产生冲突。然后新建两个文件夹,一个layout-land,另一个是layout-prot。

layout-land:存放横屏布局文件,如main.xml。布局名字与layout-prot的一样

layout-prot:存放竖屏布局文件,名字与layout-land的一样

剩下的事情就可以交由手机处理了,手机会自动处理横竖屏时布局之间的切换(前提是你的手机支持横竖屏,并且:设置-显示-自动旋转屏幕)

先看看两个布局文件吧,Activity可以不用管

layout-land/main.xml

  1. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="match_parent"
  3.     android:layout_height="match_parent"
  4.     android:orientation="horizontal" >

  5.     <linearlayout
  6.         android:orientation="vertical"
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="wrap_content"
  9.         android:layout_gravity="center"
  10.         android:paddingLeft="20dip"
  11.         android:paddingRight="20dip" 
  12.         >
  13.         <textview 
  14.             android:text="cnblogs-花郎"
  15.             android:layout_width="wrap_content"
  16.                android:layout_height="wrap_content" 
  17.                android:layout_gravity="center"
  18.                android:layout_marginBottom="20dip"
  19.                android:textSize="24.5sp"
  20.             />
  21.         [table]

  22.                 <button 
  23.                     android:text="吃饭"
  24.                     />
  25.                 <button 
  26.                     android:text="睡觉"
  27.                     />
  28.             
  29.             [table]

  30.                 <button
  31.                     android:text="旅游"
  32.                     />
  33.                 <button
  34.                     android:text="盗墓"
  35.                     />
  36.             </button
  37. </button

  38.         
  39.     

复制代码
layout-prot/main.xml

  1. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent"
  3.     android:layout_height="fill_parent"
  4.     android:background="#3500ffff"
  5.     android:padding="30dip"
  6.     android:orientation="horizontal" >

  7.     <linearlayout
  8.         android:orientation="vertical"
  9.         android:layout_height="wrap_content"
  10.         android:layout_width="fill_parent"
  11.         android:layout_gravity="center"
  12.         >
  13.         <textview 
  14.             android:text="cnblogs-花郎"
  15.             android:layout_width="wrap_content"
  16.             android:layout_height="wrap_content"
  17.             android:layout_gravity="center"
  18.             android:layout_marginBottom="25dip"
  19.             android:textSize="24.5sp"
  20.             />
  21.         </linearlayout
  22. <button 
  23.             android:layout_width="fill_parent"
  24.             android:layout_height="wrap_content"
  25.             android:text="吃饭"
  26.             />
  27.         <button 
  28.             android:layout_width="fill_parent"
  29.             android:layout_height="wrap_content"
  30.             android:text="睡觉"
  31.             />
  32.         <button 
  33.             android:layout_width="fill_parent"
  34.             android:layout_height="wrap_content"
  35.             android:text="旅游"
  36.             />
  37.         <button 
  38.             android:layout_width="fill_parent"
  39.             android:layout_height="wrap_content"
  40.             android:text="盗墓"
  41.             />
  42.     

复制代码
下面有图有真相,是横屏时的布局。
 
注:切换模拟器的命令式CTRL+F12
这里有一点需要注意的是,横竖屏切换的生命周期的问题。
在上面的那种情况下,横竖屏切换的时候,会销毁Activity后然后再次创建。
在不加入任何设置的时候,它的生命周期是这样的:
onCreate-onStart-onResume   开始运行时
onPause-onStop-onDestroy-onCreate-onStart-onResume   横竖屏切换的时候都是这样
当在AndroidManifest.xml中的activity标签中加入了:
android:configChanges="orientation|keyboardHidden"
或者android:configChanges="orientation"后
横竖屏切换就不需要重新载入了,也就是说不用销毁Activity后再重新创建Activity了。

另外一种横竖屏切换时生命周期的样子是另外的样子,是与onConfigureChanges方法有关的,它的生命周期跟我的有些不同呢?参考: android横竖屏切换时Activity的生命周期

二、如何限定横屏或者竖屏?
  有些人讨厌玩手机的时候横竖屏来回的切换,有些应用也限定了应用程序只使用横屏或者只使用竖屏,即使手机设置了“自动切换横竖屏”。比如“水果忍者”是不能竖屏的(双人模式除外了)
解决办法:只需要在AndroidManifest.xml的Activity标签中加入:android:screenOrientation="landscape"
  1. android:screenOrientation="landscape"表示横屏或android:screenOrientation="protrait"表示竖屏
复制代码
这样,所设定的应用程序就只能是横屏或者竖屏了


三,横竖屏切换时关于Activity重新载入的问题(onConfigurationChanged()方法)
例如上面的那个例子,Activity每次横竖屏切换的时候是重新载入的,但是比如我们在玩游戏的时候切换了一下屏幕,我们不可能要重新玩起,所以需要有一种解决横竖屏切换的时候保存当前状态,不用重新载入的方法
解决方案:可以使用onConfigurationChanged方法,该方法可以在用户切换横竖屏的时候就不用重新执行onCreate方法了
提醒:这个方法的使用场景比如播放影音的时候转换了一下屏幕,如果是分别设置了两个布局的话,那么横竖屏要对应不同布局,也意味着还是要执行onCreate方法,所以布局最好是一个(不知对不对,求高手指点)
1、需要在AndroidManifest.xml中的对应的activity标签里加入
  1. android:configChanges="orientation|keyboardHidden"
复制代码
这条语句的意思是:横竖屏切换或者实体键盘推出合上的时候配置信息的改变
2、需要在AndroidManifest.xml中加入权限

复制代码
3、需要在横竖屏切换时共用的那个Activity里覆盖onConfigurationChanged方法,如下
  1. @Override
  2.     public void onConfigurationChanged(Configuration newConfig) {
  3.         // TODO Auto-generated method stub
  4.         super.onConfigurationChanged(newConfig);
  5.         if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE )
  6.         {
  7.             Toast.makeText(getApplicationContext(), "切换为横屏", Toast.LENGTH_SHORT).show();
  8.         }else if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
  9.         {
  10.             Toast.makeText(getApplicationContext(), "切换为竖屏", Toast.LENGTH_SHORT).show();
  11.         }
  12.     }
复制代码
这里需要说的事,代码中的if语句是判断当前设备是横屏还是竖屏,然后有其对应的操作。之前竟然在屏幕切换的时候设置不同的布局,虽然能够显示不同的布局,但是这个方法就已经毫无意义了,因为横竖屏切换到不同的布局我们可以用上面的第一种方法,而这种最好只是对应一个布局吧,然后在里面进行横竖屏时候的其他操作,防止了重新载入

下面还是看一个例子吧,下面的是一个播放rtsp流的的例子
  1. package com.loulijun.demo07;

  2. import android.app.Activity;
  3. import android.content.res.Configuration;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.view.Window;
  7. import android.view.WindowManager;
  8. import android.widget.Toast;
  9. import android.widget.VideoView;

  10. public class Demo07Activity extends Activity {
  11.     private VideoView video;
  12.     private String rtspUrl = "rtsp://218.205.231.149:554/mobile/1/2CBE124B67C85A59/48f313651199829e.sdp?id=guest&t=1305313158&en=f2ed024c7963e179f65c65689fdd9887&rs=wap";
  13.     @Override
  14.     public void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.main);
  17.         video = (VideoView)findViewById(R.id.play);
  18.         video.setVideoURI(Uri.parse(rtspUrl));
  19.         video.requestFocus();
  20.         video.start();
  21.     }

  22.     @Override
  23.     public void onConfigurationChanged(Configuration newConfig) {
  24.         // TODO Auto-generated method stub
  25.         super.onConfigurationChanged(newConfig);
  26.         if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE )
  27.         {
  28.             Toast.makeText(getApplicationContext(), "切换为横屏", Toast.LENGTH_SHORT).show();
  29.         }else if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
  30.         {
  31.             Toast.makeText(getApplicationContext(), "切换为竖屏", Toast.LENGTH_SHORT).show();
  32.         }
  33.     }
  34.     
  35.     
  36. }
复制代码
main.xml

  1. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent"
  3.     android:layout_height="fill_parent"
  4.     android:orientation="vertical" >
  5.     
  6.     <videoview 
  7.         android:id="@+id/play"
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="wrap_content"
  10.         android:layout_gravity="center"
  11.         />

复制代码
这里需要说一下VideoView的全屏显示的问题,横屏后右边总是空出一块黑色区域,即使通过WindowManager的方式也不能解决,索性只能设置布局为居中显示了,至少好看些
所以只是在清单文件中加入了样式:android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
这种方式在播放视频的时候不能全屏,希望大牛们可以提出自己的解决方案
AndroidManifest.xml

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     package="com.loulijun.demo07"
  3.     android:versionCode="1"
  4.     android:versionName="1.0" >

  5.     

  6.     <application
  7.         android:icon="@drawable/ic_launcher"
  8.         android:label="@string/app_name" >
  9.         <activity
  10.             android:label="@string/app_name"
  11.             android:configChanges="orientation|keyboardHidden"
  12.             android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
  13.             android:name=".Demo07Activity" >
  14.             
  15.                 

  16.                 
  17.             
  18.         
  19.     
  20.     
  21.     
  22.     
  23.     
  24.     
  25. </activity
  26. </application
复制代码
看看运行效果吧
           
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值