如何改变Activity的显示方向

本示例演示如何通过Activity了的setRequestedOrientation()方法来设定Activity的显示方向。

本示例在Eclipse上编译测试。

1.  定义清单文件(AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="my.android.test"

      android:versionCode="1"

      android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".ScreenOrientation"

                  android:label="@string/app_name">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

    <uses-sdk android:minSdkVersion="9" />

</manifest>

2. 定义字符串资源(strings.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="hello">Hello World, ScreenOrientation!</string>

    <string name="app_name">ScreenOrientation</string>

    <string name="screen_orientation_summary">Demonstrates the available screen

        orientation modes.  Often you want to set the desired mode in your manifest

        instead of programmatically.</string>

    <string name="screen_orientation">Screen Orientation</string>

</resources>

3. 定义数组资源(arrays.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <!-- 下列控件应用的数组资源 -->

    <string-array name="screen_orientations">

        <item>UNSPECIFIED</item>

        <item>LANDSCAPE</item>

        <item>PORTRAIT</item>

        <item>USER</item>

        <item>BEHIND</item>

        <item>SENSOR</item>

        <item>NOSENSOR</item>

        <item>SENSOR_LANDSCAPE</item>

        <item>SENSOR_PORTRAIT</item>

        <item>REVERSE_LANDSCAPE</item>

        <item>REVERSE_PORTRAIT</item>

        <item>FULL_SENSOR</item>

    </string-array>

</resources>

4. 定义布局资源(screen_orientation.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:padding="4dip"

    android:gravity="center_horizontal"

    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView

        android:layout_width="match_parent" android:layout_height="wrap_content"

        android:layout_weight="0"

        android:paddingBottom="4dip"

        android:text="@string/screen_orientation_summary"/>

    <Spinner android:id="@+id/orientation"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:drawSelectorOnTop="true"

        android:prompt="@string/screen_orientation">

    </Spinner>

</LinearLayout>

5. 创建Activity类(ScreenOrientation.java

package my.android.test;

 

import android.app.Activity;

import android.content.pm.ActivityInfo;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.AdapterView.OnItemSelectedListener;

 

publicclass ScreenOrientation extends Activity {

    //Activity中的调整列表。

    Spinner mOrientation;

    /**

     * 设定Activity主窗口的方向,数组中的方向会设定给R.attr类中的screenOrientation属性,

     * screenOrientation的属性值必须是以下常量值。

     * ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED

     * 不指定方向,让系统决定Activity的最佳方向。

     * ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

     * 希望Activity在横向屏上显示,也就是说横向的宽度要大于纵向的高度,并且忽略方向传感器的影响。

     * ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

     * 希望Activity在纵向屏上显示,也就是说纵向的高度要大于横向的宽度,并且忽略方向传感器的影响。

     * ActivityInfo.SCREEN_ORIENTATION_USER

     * 使用用户设备的当前首选方向。

     * ActivityInfo.SCREEN_ORIENTATION_BEHIND

     * 始终保持与屏幕一致的方向,不管这个Activity在前台还是后台。

     * ActivityInfo.SCREEN_ORIENTATION_SENSOR

     * Activity的方向由物理方向传感器来决定,按照用户旋转设备的方向来显示。

     * ActivityInfo.SCREEN_ORIENTATION_NOSENSOR

     * 始终忽略方向传感器的判断,当用户旋转设备时,显示不跟着旋转。

     * ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE

     * 希望Activity在横向屏幕上显示,但是可以根据方向传感器指示的方向来进行改变。

     * ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT

     * 希望Activity在纵向屏幕上显示,但是可以根据方向传感器指示的方向来进行改变。

     * ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE

     * 希望Activity在横向屏幕上显示,但与正常的横向屏幕方向相反。

     * ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT

     * 希望Activity在纵向屏幕上显示,但与正常的纵向屏幕方向相反

     * ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR:

     * Activity的方向由方向传感器来决定,显示会根据用户设备的移动情况来旋转。

     */

    finalstaticintmOrientationValues[] = newint[]{

       ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED,

       ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,

       ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,

       ActivityInfo.SCREEN_ORIENTATION_USER,

       ActivityInfo.SCREEN_ORIENTATION_BEHIND,

       ActivityInfo.SCREEN_ORIENTATION_SENSOR,

       ActivityInfo.SCREEN_ORIENTATION_NOSENSOR,

       ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE,

       ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT,

       ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE,

       ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,

       ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR

    };

   

    /** 首次创建本Activity时,调用这个方法 */

    @Override

    publicvoid onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        //填充布局

        setContentView(R.layout.screen_orientation);

        //查找下拉控件

        mOrientation = (Spinner)findViewById(R.id.orientation);

        //引用数组资源,创建字符数组适配器

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,

             R.array.screen_orientations,android.R.layout.simple_spinner_item);

        //把数组适配器与下拉控件关联

        mOrientation.setAdapter(adapter);

        //给下拉控件设置时间监听器

        mOrientation.setOnItemSelectedListener(

             /**

              * 列表项目选择监听器

              * onItemSelected:有项目选择时要执行这个回调。

              * onNothingSelected:没有选择任何项目时执行这个回调。

              */

             new OnItemSelectedListener(){

                publicvoid onItemSelected(AdapterView<?> parent, View view, int position, long id){

                    //设置Activity的方向选项

                    setRequestedOrientation(mOrientationValues[position]);

                }

                publicvoid onNothingSelected(AdapterView<?> parent){

                    //不特定指定方向

                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);                     

                }

             });

    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值