android--Toggle Screen Orientation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.