【Android】4.3 屏幕布局和旋转

分类:C#、Android、VS2015;创建日期:2016-02-06

为了控制屏幕的放置方向(纵向、横向),可以在Resource下同时定义两种不同的布局文件夹:layout和layout-land,这样一来,系统就会根据当前屏幕的放置方向自动请求合适的布局。

注意:<Ctrl>+<F11>是控制模拟器“竖屏/横屏”转换的快捷键。

1、layout和layout-land

要点:

纵向放置方式(portrait,肖像模式)使用的资源保存在layout文件夹下。

横向放置(landscape,景观模式)使用的资源保存在layout-land文件夹下。

默认情况下,当旋转屏幕时,文字会自动旋转(见layout文件夹下的Main.axml源代码)。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
<Button  
  android:id="@+id/myButton"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/hello"/>
</LinearLayout>

这样设置的好处是:当在设计模式下将屏幕横放时,它就会自动旋转文字。

除了布局文件夹默认的纵向放置方式(portrait)以外,还可以命名一个layout-land文件夹(意为landscape)让其横向放置,而且不需要添加任何代码。

如果layout下有一个Main.axml文件,而且layout-land下也包含一个Main.axml文件,那么,当屏幕横放时,Android就会自动加载layout-land下的Main.axml。

2、在drawable文件夹下指定旋转后使用的绘制资源

与layout和layout-land相似,如果将纵向屏幕和横向屏幕使用的可绘制资源分别保存在Resources/drawable文件夹和Resources/drawable-land文件夹下,旋转屏幕方向时系统同样会自动获取相应的资源文件。例如,在Resources/drawable文件夹下有一个Monkey.png文件,XML描述如下:

<ImageView
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:src="@drawable/monkey"
  android:layout_centerVertical="true"
  android:layout_centerHorizontal="true" />

如果Resources/drawable-land下也有一个Monkey.png,那么,当屏幕横向放置时,它就会自动呈现Resources/drawable-land下的Monkey.png,如下图所示。

3、通过程序控制屏幕旋转

有时我们可能需要在代码中定义布局。与使用XML资源时系统会自动处理屏幕放置不同,当以编程方式添加控件时,必须考虑控件放置的方向。即:必须执行下面的步骤:

  • 创建布局。
  • 设置布局参数。.
  • 创建控件。
  • 设置控件的布局参数。
  • 添加控件到布局中。
  • 将布局作为视图来呈现。

例如,下面的代码将一个TextView添加到RelativeLayout中:

protected override void OnCreate (Bundle bundle)
{
  base.OnCreate (bundle);

  var rl = new RelativeLayout (this);
  var layoutParams = new RelativeLayout.LayoutParams (
          ViewGroup.LayoutParams.FillParent,
          ViewGroup.LayoutParams.FillParent);
  rl.LayoutParameters = layoutParams;

  var tv = new TextView (this);
  tv.LayoutParameters = layoutParams;
  tv.Text = "Programmatic layout";

  rl.AddView (tv);
  SetContentView (rl);
}

下图是运行效果:

 

image

4、在代码中判断屏幕放置方式

Android提供了一个WindowManager类,在C#代码中,可在OnCreate中通过WindowManager.DefaultDisplay.Rotation属性确定当前设备的放置方向,如下所示:

protected override void OnCreate (Bundle bundle)
{
  base.OnCreate (bundle);

  var rl = new RelativeLayout (this);
  var layoutParams = new RelativeLayout.LayoutParams (
        ViewGroup.LayoutParams.FillParent,
        ViewGroup.LayoutParams.FillParent);
  rl.LayoutParameters = layoutParams;

  var surfaceOrientation = WindowManager.DefaultDisplay.Rotation;
  // create layout based upon orientation
  RelativeLayout.LayoutParams tvLayoutParams;

  if (surfaceOrientation == SurfaceOrientation.Rotation0 ||
       surfaceOrientation == SurfaceOrientation.Rotation180)
  {
    tvLayoutParams = new RelativeLayout.LayoutParams (
           ViewGroup.LayoutParams.FillParent,
           ViewGroup.LayoutParams.WrapContent);
  }
  else
  {
    tvLayoutParams = new RelativeLayout.LayoutParams (
           ViewGroup.LayoutParams.FillParent,
           ViewGroup.LayoutParams.WrapContent);
    tvLayoutParams.LeftMargin = 100;
    tvLayoutParams.TopMargin = 100;
  }

  var tv = new TextView (this);
  tv.LayoutParameters = tvLayoutParams;
  tv.Text = "Programmatic layout";
  rl.AddView (tv);
  SetContentView (rl);
}

当将屏幕从纵向旋转为横向时,运行效果如下图所示:

image

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值