Main.xml Code:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" > <button android:id= "@+id/mybtn" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Change the orientation of the screen (currently displayed as a vertical screen)"/> <imageview Android:id= "@+id/myview" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:src= "@drawable/aa"/></linearlayout>
. Java code is as follows:
Package Org.lxh.demo;import Android.app.activity;import Android.app.alertdialog;import android.app.Dialog;import Android.content.dialoginterface;import Android.content.pm.activityinfo;import Android.os.Bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.view.onfocuschangelistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.imageview;import Android.widget.textview;public class Hello extends Activity {private Button change = Null;private ImageView img = null;pub LIC void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//life cycle Method Super.setcontentview ( R.layout.main); Set the layout manager to use This.change = (Button) Super.findviewbyid (r.id.mybtn); this.img = (ImageView) Super.findviewbyid ( R.id.myview); This.change.setOnClickListener (new Onclicklistenerimpl ());} Private class Onclicklistenerimpl implements Onclicklistener {public void OnClick (View arg0) {if ( Hello.this.getRequestedOrientation () = = ActivityiNfo. screen_orientation_unspecified) {Hello.this.change.setText ("screen orientation cannot be performed");} else {if ( Hello.this.getRequestedOrientation () = = Activityinfo.screen_orientation_landscape) { Hello.this.setRequestedOrientation (activityinfo.screen_orientation_portrait);} else if (Hello.this.getRequestedOrientation () = = activityinfo.screen_orientation_portrait) { Hello.this.setRequestedOrientation (Activityinfo.screen_orientation_landscape);}}}}
You need to configure permissions:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Org.lxh.demo "android:versioncode=" 1 "android:versionname=" 1.0 "><USES-SDK android: minsdkversion= "/><strong><span style=" color: #ff0000; " ><uses-permission android:name= "Android.permission.CHANGE_CONFIGURATION"/></span></strong> <application android:icon= "@drawable/icon" android:label= "@string/app_name" ><activity android:name= ". Hello "android:label=" @string/app_name "<strong><span style=" color: #ff0000; " >android:configchanges= "Orientation|keyboard" android:screenorientation= "Portrait" </span></ Strong>><intent-filter><action android:name= "Android.intent.action.MAIN"/><category Android : Name= "Android.intent.category.LAUNCHER"/></intent-filter></activity></application></ Manifest>
android--Toggle Screen Orientation