MonoDroid学习笔记(十四)—— 动态更改屏幕方向

Android手机有个比较有趣的功能,就是当你把手机横过来的时候,手机的内容也会跟着横过来。那么要如何通过程序控制Activity的显示方向呢?在MonoDroid里,若要通过程序改变屏幕显示的方向,只要调用SetRequestedOrientation方法即可,而若要取得当前屏幕的方向,则使用RequestedOrientation属性即可。

本范例很简单,界面只有一个按钮,点击它的时候判断当前屏幕方向,如果是竖屏(Portrait),则将其改为横屏(Landscape),反之亦然。布局文件如下:

[xhtml]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.                 android:layout_width="fill_parent"  
  4.                 android:layout_height="fill_parent">  
  5.   <Button android:id="@+id/btnLandScape"  
  6.           android:text="按我旋转屏幕"  
  7.           android:layout_width="wrap_content"  
  8.           android:layout_height="wrap_content"/>  
  9.     
  10. </RelativeLayout>  
 

程序也很简单,只要在Button的单击事件中判断当前的屏幕方向,然后调用SetRequestedOrientation方法:

[c-sharp]  view plain copy print ?
  1. protected override void OnCreate(Bundle bundle)  
  2. {  
  3.     base.OnCreate(bundle);  
  4.     SetContentView(Resource.Layout.Main);  
  5.     Button btnLandScape = FindViewById<Button>(Resource.Id.btnLandScape);  
  6.     btnLandScape.Click += (sender, e) =>  
  7.     {  
  8.         if (this.RequestedOrientation == (int)Android.Content.PM.ScreenOrientation.Landscape)  
  9.         {  
  10.             this.SetRequestedOrientation(Android.Content.PM.ScreenOrientation.Portrait);  
  11.             Toast.MakeText(this"旋转为竖屏", ToastLength.Long).Show();  
  12.         }  
  13.         else if (this.RequestedOrientation == (int)Android.Content.PM.ScreenOrientation.Portrait)  
  14.         {  
  15.             this.SetRequestedOrientation(Android.Content.PM.ScreenOrientation.Landscape);  
  16.             Toast.MakeText(this"旋转为横屏", ToastLength.Long).Show();  
  17.         }  
  18.     };  
  19. }  
 

在模拟器里运行程序,点击按钮,咦?怎么没有反应?跟踪了一下代码一看,发现this.RequestedOrientation竟然是-1!,这是怎么回事儿呢?原来,在Android里,如果我们不在AndroidManifest.xml当中设置Activity的android:screenOrientation属性,否则即使程序运行时屏幕是竖屏的,this.RequestedOrientation也默认为-1。然而,还记得吗,在第十篇笔记里我们讲过,在MonoDroid里,AndroidManifest.xml中Activity的属性我们可以通过为Activity类打上Attibute标记来实现,所以我们只要在我们的Activity1类的Attribute里加上ScreenOrientation属性即可:

[c-sharp]  view plain copy print ?
  1. [Activity(Label = "MonoDroidTest", MainLauncher = true,  ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]  
  2. public class Activity1 : Activity  
 

重新运行程序,点击按钮,效果出现了:

如果不想在Activity中添加ScreenOrientation这个Attribute,我们可以用另外一种方法来判断当前的屏幕方向,那就是判断屏幕的宽度和高度,如果宽度比高度大,则是横屏,否则就是竖屏。

[c-sharp]  view plain copy print ?
  1. protected override void OnCreate(Bundle bundle)  
  2. {  
  3.     base.OnCreate(bundle);  
  4.     SetContentView(Resource.Layout.Main);  
  5.     Button btnLandScape = FindViewById<Button>(Resource.Id.btnLandScape);  
  6.     btnLandScape.Click += (sender, e) =>  
  7.     {  
  8.         Display d = this.Window.WindowManager.DefaultDisplay;  
  9.         int h = d.Height;  
  10.         int w = d.Width;  
  11.         if (w > h)  
  12.         {  
  13.             this.SetRequestedOrientation(Android.Content.PM.ScreenOrientation.Portrait);  
  14.         }  
  15.         else  
  16.         {  
  17.             this.SetRequestedOrientation(Android.Content.PM.ScreenOrientation.Landscape);  
  18.         }  
  19.     };  
  20. }  
 

效果与上图一样。改变屏幕方向我们是实现了,那么当屏幕方向发生改变时,是否有事件触发呢?肯定是有的,我们只需重写OnConfigurationChanged方法即可。这个方法是当系统发生系统设置改变之后所触发的,其中唯一的传入参数为Configuration对象,除了可处理屏幕设置更改事件之外,也可以处理其他系统设置变更事件,如隐藏键盘或打开键盘等,大家可以试试。

[c-sharp]  view plain copy print ?
  1. public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)  
  2. {  
  3.     Display d = this.Window.WindowManager.DefaultDisplay;  
  4.     int h = d.Height;  
  5.     int w = d.Width;  
  6.     if (newConfig.Orientation == Android.Content.Res.Orientation.Landscape)  
  7.     {  
  8.         Toast.MakeText(thisstring.Format("旋转为横屏,分辨率为{0}×{1}", w, h), ToastLength.Long).Show();  
  9.     }  
  10.     else if (newConfig.Orientation == Android.Content.Res.Orientation.Portrait)  
  11.     {  
  12.         Toast.MakeText(thisstring.Format("旋转为竖屏,分辨率为{0}×{1}", w, h), ToastLength.Long).Show();  
  13.     }  
  14.     base.OnConfigurationChanged(newConfig);  
  15. }  
 

运行程序,还是没有达到效果,是为嘛捏?原来我们要使OnConfigurationChanged生效,必须在Activity上打上ConfigurationChanges这个Attribute,并且它的值为Android.Content.PM.ConfigChanges枚举的一个并集,例如我们这里要捕获屏幕更改,就必须赋上Android.Content.PM.ConfigChanges.Orientation,如果同时还要捕获键盘的更改事件,必须赋上:Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.Keyboard,如下:

[c-sharp]  view plain copy print ?
  1. [Activity(Label = "MonoDroidTest", MainLauncher = true, ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.Keyboard)]  
  2. public class Activity1 : Activity  
  3. {  
 

我们再重新运行程序,这下效果就出来了:

最后告诉大家一个小诀窍,如果要更改模拟器的方向,我们可以按Ctrl+F11来模拟:

转载http://blog.csdn.net/ojlovecd/article/details/6402534

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值