---
title: "Android 基础教程之---动态更改屏幕方向的简单例子(LANDSCAPE与PORTRAIT)!"
author: "mmdev"
date: "2009-12-17T22:10:00Z"
tags: []
slug: "android-landscape-portrait"
draft: false
lastmod: "2009-12-17T22:10:00Z"
id: 990396
url: "https://mmdev.iteye.com/blog/990396"
---

大家好，今天要讲的是**Android**手机如何动态手机屏幕方向的，我们当中有可能手机也会有这种功能，当我们手机方向改变时，屏幕也会跟着改变，在这**Android**当中是很容易实现的．本节的**Demo**主要是界面有一个按钮，当点击时，如果屏幕方向是横排(**PORTRAIT**)刚将屏幕方向更改为竖排(**LANDSCAPE**),反之依然！我们这里主要是运用了**getRequestedOrientation**(),和**setRequestedorientation**()两个方法．但是要利用这两个方法必须先在**AndroidManiefst.xml**设置一下屏幕方属性，不然程序将不能正常的工作．下面我将分为N个步骤一步一步教你如何实现该**Demo**.

**Step 1:**我们建立一个**Android**工程,命名为**ChangeOrientationDemo**.

**Step 2**:设计**UI**,打开**main.xml**,将其代码修改如下，我们这里只是增加了一个按钮，其他什么都没有动．

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;LinearLayout xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"  
android:orientation="vertical"  
android:layout\_width="fill\_parent"  
android:layout\_height="fill\_parent"  
&gt;  
&lt;TextView  
android:layout\_width="fill\_parent"  
android:layout\_height="wrap\_content"  
android:text="@string/hello"  
/&gt;  
**&lt;Button**  
**android:id="@+id/bt1"**  
**android:layout\_width="wrap\_content"**  
**android:layout\_height="wrap\_content"**  
**android:text="Press me Change Orientation"**  
**/&gt;**  
&lt;/LinearLayout&gt;

**Step 3**:设计主程序**ChangeOrientationDemo.java**,修改其代码如下:

package com.android.test;

import android.app.Activity;  
**import android.content.pm.ActivityInfo;**  
import android.os.Bundle;  
**import android.view.View;**  
**import android.widget.Button;**

public class ChangeOrientationDemo extends Activity {

**private Button bt1;**

public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
setContentView(R.layout.main);

**//获取资源**  
**bt1 = (Button)findViewById(R.id.bt1);**  
**//增加按钮事件**  
**bt1.setOnClickListener(new Button.OnClickListener(){**

**@Override**  
**public void onClick(View v) {**  
**//如果是竖排,则改为横排**  
**if(getRequestedOrientation() == ActivityInfo.SCREEN\_ORIENTATION\_LANDSCAPE)**  
**{**  
**setRequestedOrientation(ActivityInfo.SCREEN\_ORIENTATION\_PORTRAIT);**  
**}**

**//如果是横排,则改为竖排**

**else if(getRequestedOrientation() == ActivityInfo.SCREEN\_ORIENTATION\_PORTRAIT)**  
**{**  
**setRequestedOrientation(ActivityInfo.SCREEN\_ORIENTATION\_LANDSCAPE);**  
**}**  
**}**

**});**  
}

}

**Step 4**:在**AndroidManifest.xml**文件里设置默认方向，不然程序不能正常工作哦．代码如下：

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;manifest xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"  
package="com.android.test"  
android:versionCode="1"  
android:versionName="1.0"&gt;  
&lt;application android:icon="@drawable/icon" android:label="@string/app\_name"&gt;  
&lt;activity android:name=".ChangeOrientationDemo"  
android:label="@string/app\_name"  
**android:screenOrientation="portrait"&gt;**  
&lt;intent-filter&gt;  
&lt;action android:name="android.intent.action.MAIN" /&gt;  
&lt;category android:name="android.intent.category.LAUNCHER" /&gt;  
&lt;/intent-filter&gt;  
&lt;/activity&gt;

&lt;/application&gt;  
&lt;uses-sdk android:minSdkVersion="3" /&gt;

&lt;/manifest&gt;

**Step 5:**运行程序，效果如下图:

![](http://p.blog.csdn.net/images/p_blog_csdn_net/Android_Tutor/EntryImages/20091217/ch1.jpg)![](http://p.blog.csdn.net/images/p_blog_csdn_net/Android_Tutor/EntryImages/20091217/ch2.jpg)

OK,今天就到这里，谢谢大家！