大家好,今天要讲的是Android手机如何动态手机屏幕方向的,我们当中有可能手机也会有这种功能,当我们手机方向改变时,屏幕也会跟着改变,在这Android当中是很容易实现的.本节的Demo主要是界面有一个按钮,当点击时,如果屏幕方向是横排(PORTRAIT)刚将屏幕方向更改为竖排(LANDSCAPE),反之依然!我们这里主要是运用了getRequestedOrientation(),和setRequestedorientation()两个方法.但是要利用这两个方法必须先在AndroidManiefst.xml设置一下屏幕方属性,不然程序将不能正常的工作.下面我将分为N个步骤一步一步教你如何实现该Demo.
Step 1:我们建立一个Android工程,命名为ChangeOrientationDemo.
Step 2:设计UI,打开main.xml,将其代码修改如下,我们这里只是增加了一个按钮,其他什么都没有动.
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press me Change Orientation"
/>
</LinearLayout>
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文件里设置默认方向,不然程序不能正常工作哦.代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ChangeOrientationDemo"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
Step 5:运行程序,效果如下图:
OK,今天就到这里,谢谢大家!
分享到:
相关推荐
Android读写XML(上)——package说明.doc Android读写XML(下)——创建XML文档.doc ...Android基础教程之----动态更改屏幕方向的简单例子(LANDSCAPE与PORTRAIT)! .doc Android基础教程之----五大布局对象.doc
Android 屏幕旋转实例,改变屏幕方向,这个和平时的锁定屏幕方向有关联,其基本的实现思路如下: public void onConfigurationChanged(Configuration newConfig) { Toast.makeText(this, "系统的屏幕方向发生...
ZXing Android Embedded Barcode scanning library for ...Scanning can be performed in landscape or portrait mode. Camera is managed in a background thread, for fast startup time. A sample application
FolioReader-Android is an ePub reader and parser framework written in Java. Features [x] Custom Fonts [x] Custom Text Size [x] Themes / Day mode / Night mode [x] Text Highlighting [x] List / Edit / ...
Hello Camera2 ============= Hello Camera2 is an Android Camera2 sample that plays preview. It controls camera devices by NDK camera2 APIs ... (e.g when switching portrait to landscape mode, vice versa)
在AndroidManifest.xml的activity(需要禁止转向的activity)配置中加入android:screenOrientation=”landscape”属性即可(landscape是横向,portrait是纵向)。例如: <?xml version=1.0 encoding=utf-8?> &...
(跨平台支持)特征将屏幕锁定方向为PORTRAIT | LANDSCAPE-LEFT | PORTRAIT-UPSIDEDOWN | LANDSCAPE-RIGHT。 听设备的方向变化获取设备的当前方向变更日志ChangeLog详细信息v1.2.0突破性变化添加对...
5.22 动态更改屏幕方向——LANDSCAPE与PORTRAIT 5.23 系统设置更改事件——onConfigurationChanged信息处理 5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 ...
5.22 动态更改屏幕方向——LANDSCAPE与PORTRAIT 5.23 系统设置更改事件——onConfigurationChanged信息处理 5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 ...
5.22 动态更改屏幕方向——LANDSCAPE与PORTRAIT 5.23 系统设置更改事件——onConfigurationChanged信息处理 5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 ...
key_support_scene_mode = auto,auto,portrait,landscape,night,night-portrait,theatre,beach,snow,sunset,steadyphoto,fireworks,sports,party,candlelight,barcode key_default_scene_mode = auto used_white_...
key_support_scene_mode = auto,portrait,landscape,night,night-portrait,theatre,beach,snow,sunset,steadyphoto,fireworks,sports,party,candlelight,barcode key_default_scene_mode = auto used_white_balance...
zxing-barcode A barcode scanner based on zxing Support screen auto rotate (both Landscape and portrail) Simplify Zxing example code for android Highly custom FinderView
key_support_scene_mode = auto,portrait,landscape,night,night-portrait,theatre,beach,snow,sunset,steadyphoto,fireworks,sports,party,candlelight,barcode key_default_scene_mode = auto used_white_balance...
key_support_scene_mode = auto,portrait,landscape,night,night-portrait,theatre,beach,snow,sunset,steadyphoto,fireworks,sports,party,candlelight,barcode key_default_scene_mode = auto used_white_balance...
Every Android application should provide alternative resources to support specific device configurations such as portrait, landscape, small, large, us, fr, ... Because we don't want to edit thousands ...
key_support_scene_mode = auto,portrait,landscape,night,night-portrait,theatre,beach,snow,sunset,steadyphoto,fireworks,sports,party,candlelight,barcode key_default_scene_mode = auto used_white_balance...
很多开发Android的网友可能需要判断当前的屏幕方向或键盘状态,下面的代码可以判断出横屏landscape和常规的portrait纵握方式,如果使用的是G1这样有QWERTY键盘硬件的,还可以判断屏幕方向以及键盘的拉出状态。
Android横竖屏切换,包含代码中横屏 180 旋转 ,竖屏180 旋转; //横屏正方向 if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { setRequestedOrientation(ActivityInfo.SCREEN_...