手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android改变手机屏幕朝向的方法
Android改变手机屏幕朝向的方法
摘要:本文实例讲述了Android改变手机屏幕朝向的方法。分享给大家供大家参考。具体如下:模拟当点击按钮时,使手机朝向发生改变。main.xml布...

本文实例讲述了Android改变手机屏幕朝向的方法。分享给大家供大家参考。具体如下:

模拟当点击按钮时,使手机朝向发生改变。

main.xml布局文件:

<"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/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="点击更改屏幕朝向" /> <> <EditText android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cursorVisible="false" android:hint="显示当前屏幕朝向" /> </LinearLayout>

清单文件:

<"1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ljq.activity" android:versionCode="1" android:versionName="1.0"> <!-- 设置手机的朝向,不然无法获取手机的朝向 android:screenOrientation="portrait" android:configChanges="orientation" --> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".OrientationActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="orientation"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> <> <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> </manifest>

OrientationActivity类:

package com.ljq.activity; import android.app.Activity; import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class OrientationActivity extends Activity { private EditText editText=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText=(EditText)findViewById(R.id.editText); Button btn=(Button)findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { //判断是否可以获得requestedOrientation属性 if(OrientationActivity.this.getRequestedOrientation()==-1){ Toast.makeText(OrientationActivity.this, "系统的朝向无法获取", Toast.LENGTH_LONG).show(); }else{ //手机屏幕的朝向有7个可选值,分别如下 //SCREEN_ORIENTATION_BEHIND: 继承Activity堆栈中当前Activity下面的那个Activity的方向 //SCREEN_ORIENTATION_LANDSCAPE: 横屏(风景照) ,显示时宽度大于高度 //SCREEN_ORIENTATION_PORTRAIT: 竖屏 (肖像照) , 显示时高度大于宽度 //SCREEN_ORIENTATION_NOSENSOR: 忽略物理感应器——即显示方向与物理感应器无关, //不管用户如何旋转设备显示方向都不会随着改变("unspecified"设置除外) //SCREEN_ORIENTATION_SENSOR: 由物理感应器决定显示方向,它取决于用户如何持有设备, //当设备被旋转时方向会随之变化——在横屏与竖屏之间 //SCREEN_ORIENTATION_UNSPECIFIED: 未指定,此为默认值,由Android系统自己选择适当的方向, //选择策略视具体设备的配置情况而定,因此不同的设备会有不同的方向选择 //SCREEN_ORIENTATION_USER: 用户当前的首选方向 if(OrientationActivity.this.getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){ OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); }else if(OrientationActivity.this.getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){ OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } } }); } /** * 配置信息发生改变时触发 */ @Override public void onConfigurationChanged(Configuration newConfig) { Toast.makeText(this, "系统的屏幕方向发生改变", Toast.LENGTH_LONG).show(); int o=getRequestedOrientation();//获取手机的朝向 switch (o) { case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: editText.setText("当前屏幕朝向为: 横屏"); break; case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: editText.setText("当前屏幕朝向为: 竖屏"); break; } //不能省略,否则会报android.app.SuperNotCalledException: Activity OrientationActivity did not //call through to super.onConfigurationChanged()异常 super.onConfigurationChanged(newConfig); } }

运行结果:

Android改变手机屏幕朝向的方法1

希望本文所述对大家的Android程序设计有所帮助。

【Android改变手机屏幕朝向的方法】相关文章:

Android 如何收集已发布程序的崩溃信息

android开发环境遇到adt无法启动的问题分析及解决方法

Android支持的媒体格式

Android判断屏幕是横屏或是竖屏

Android APK文件在电脑(PC虚拟机)上面运行方法

android计时器,时间计算器的实现方法

android图库竖屏不显示status bar的解决方法

Android开发:消息机制简述

android动态布局之动态加入TextView和ListView的方法

Android手机内存中文件的读写方法小结

精品推荐
分类导航