玩转Android之设备方向

来源:互联网 发布:虚拟漫游软件 编辑:程序博客网 时间:2024/06/06 02:15

今天我们来聊一下设备的方向问题。我们主要说的设备方向主要是横向(Landscape)和竖直(Portrait)。设备的方向会影响到很多东西,最主要的就是设备的布局。所以,某些时候你可以能不希望设备方向变了以后布局的方向也跟着改变。

监测方向改变

首先创建一个叫做OrientationActivity的Activity,作为我们的示例。

有两个方法可以监测到当前设备方向的改变。

一、手动处理屏幕旋转

AndroidManifest.xml文件里给OrientationActivity添加一个配置:

    <activity        android:name=".D11.OrientationActivity"        android:configChanges="orientation|screenSize|keyboard">    </activity>

android:configChanges - 这个配置是告诉Android系统,我们要自己处理屏幕方向改变,屏幕尺寸改变和键盘的隐藏、显示。

OrientationActivity类代码中添加方法:

    @Override    public void onConfigurationChanged(Configuration newConfig) {        super.onConfigurationChanged(newConfig);        Log.i(TAG, "onConfigurationChanged " + newConfig);        if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){            Log.i(TAG, "PORTRAIT");        } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){            Log.i(TAG, "LANDSCAPE");        }    }

在上面说到的三个配置发生变化的时候,这个方法就会被调用。运行起来代码,然后旋转屏幕(如果是真机测试的话,记得把屏幕设置为可旋转)。在Logcat里就会打印出旋转的信息:

I/OrientationActivity: onConfigurationChanged {0 1.0 themeSeq = 2 showBtnBg = 0 460mcc3mnc zh_CN ldltr sw360dp w640dp h336dp 640dpi nrml long land finger -keyb/v/h -nav/h mkbd/? s.212}I/OrientationActivity: LANDSCAPEI/OrientationActivity: onConfigurationChanged {0 1.0 themeSeq = 2 showBtnBg = 0 460mcc3mnc zh_CN ldltr sw360dp w360dp h616dp 640dpi nrml long port finger -keyb/v/h -nav/h mkbd/? s.213}I/OrientationActivity: PORTRAIT

里面的一长串内容里两个单词非常的明显表明了方向。第一行的land,第二行的port,是两个单词的缩写意思是横屏竖屏

二、定

所谓的就是让屏幕在旋转的时候不要改变方向。只要在AndroidManifest.xml文件里给* OrientationActivity*节点添加android:screenOrientation="portrait"属性就可以。这样,手机怎么旋转,这个Activity始终保持竖直的方向。

    <activity        android:name=".D11.OrientationActivity"        android:configChanges="orientation|screenSize|keyboard"        android:screenOrientation="portrait">    </activity>

三、变

前面说的两个,一个是告诉系统我们要手动处理,其实什么都没有处理。只是捕捉到了当前的手机屏幕方向。第二个,是让Activity的方向不受手机方向的影响。

第二点是在处理手机方向的改变了。但是,是一种消极的处理方式。如果你的APP需要横屏显示内容怎么办?下面就来说说如何处理多个方向的Activity布局。

  1. 在res里新建一个目录叫做layout-land。这就有两个layout目录了,除了layout还有一个layout-land。-land后缀表明这个目录下的布局文件都是水平方向使用的。没有这个后缀的就是存放的就是处理竖直方向的布局文件。
  2. layout-land里添加一个布局文件,名字和layout里的一样:activity_orientation.xml
    为了在布局里区分Activity的方向,我们给两个布局文件添加不同的TextView

app/res/layout/activity_orientation.xml 竖直的

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_orientation"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.a31day.D11.OrientationActivity">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Portrait"        android:textSize="30sp" /></RelativeLayout>

app/res/layout-land/activity_orientation.xml 水平的:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_orientation"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.a31day.D11.OrientationActivity">    <TextView android:textSize="30sp" android:text="Landscape"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></RelativeLayout>

最后,记得在AndroidManifest.xml文件里把OrientationActivity恢复到开始状态。否则,我们上面的修改是不起作用的。

<activity android:name=".D11.OrientationActivity"></activity>

这样就都搞定了。运行起来就可以看到效果了。

0 0
原创粉丝点击