How to change the Activity display direction

Source: Internet
Author: User

This example shows how to set the display direction of an Activity through the setRequestedOrientation () method of the Activity.

This example is compiled and tested on Eclipse.

1. Define the inventory file (AndroidManifest. xml)

<? Xml version = "1.0" encoding = "UTF-8"?>

<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"

Package = "my. android. test"

Android: versionCode = "1"

Android: versionName = "1.0" type = "codeph" text = "/codeph">

<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">

<Activity android: name = ". ScreenOrientation"

Android: label = "@ string/app_name">

<Intent-filter>

<Action android: name = "android. intent. action. MAIN"/>

<Category android: name = "android. intent. category. LAUNCHER"/>

</Intent-filter>

</Activity>

</Application>

<Uses-sdk android: minSdkVersion = "9"/>

</Manifest>

2. Define string resources (strings. xml)

<? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>

<String name = "hello"> Hello World, ScreenOrientation! </String>

<String name = "app_name"> ScreenOrientation </string>

<String name = "screen_orientation_summary"> Demonstrates the available screen

Orientation modes. Often you want to set the desired mode in your manifest

Instead of programmatically. </string>

<String name = "screen_orientation"> Screen Orientation </string>

</Resources>

3. Define an array Resource (arrays. xml)

<? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>

<! -- Array resources applied by the following controls -->

<String-array name = "screen_orientations">

<Item> UNSPECIFIED </item>

<Item> LANDSCAPE </item>

<Item> PORTRAIT </item>

<Item> USER </item>

<Item> BEHIND </item>

<Item> SENSOR </item>

<Item> NOSENSOR </item>

<Item> SENSOR_LANDSCAPE </item>

<Item> SENSOR_PORTRAIT </item>

<Item> REVERSE_LANDSCAPE </item>

<Item> REVERSE_PORTRAIT </item>

<Item> FULL_SENSOR </item>

</String-array>

</Resources>

4. Define layout resources (screen_orientation.xml)

<? Xml version = "1.0" encoding = "UTF-8"?>

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: orientation = "vertical" android: padding = "4dip"

Android: gravity = "center_horizontal"

Android: layout_width = "match_parent" android: layout_height = "match_parent">

<TextView

Android: layout_width = "match_parent" android: layout_height = "wrap_content"

Android: layout_weight = "0"

Android: paddingBottom = "4dip"

Android: text = "@ string/screen_orientation_summary"/>

<Spinner android: id = "@ + id/orientation"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: drawselectid Top = "true"

Android: prompt = "@ string/screen_orientation">

</Spinner>

</LinearLayout>

5. Create Activity class (ScreenOrientation. java)

Package my. android. test;

 

Import android. app. Activity;

Import android. content. pm. ActivityInfo;

Import android. OS. Bundle;

Import android. view. View;

Import android. widget. AdapterView;

Import android. widget. ArrayAdapter;

Import android. widget. Spinner;

Import android. widget. AdapterView. OnItemSelectedListener;

 

Publicclass ScreenOrientation extends Activity {

// Adjustment list in Activity.

Spinner mOrientation;

/**

* Sets the direction of the Activity main window. The direction in the array is set to the screenOrientation attribute in the R. attr class,

* The value of screenOrientation must be a constant value.

* ActivityInfo. SCREEN_ORIENTATION_UNSPECIFIED:

* If no direction is specified, the system determines the best direction of the Activity.

* ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE:

* You want the Activity to be displayed on the horizontal screen, that is, the horizontal width must be greater than the vertical height, and the influence of the direction sensor is ignored.

* ActivityInfo. SCREEN_ORIENTATION_PORTRAIT:

* You want the Activity to be displayed on the portrait screen, that is, the portrait height must be greater than the horizontal width, and the impact of the orientation sensor is ignored.

* ActivityInfo. SCREEN_ORIENTATION_USER:

* Use the current preferred direction of your device.

* ActivityInfo. SCREEN_ORIENTATION_BEHIND:

* Always keep the same direction as the screen, no matter whether the Activity is in the foreground or background.

* ActivityInfo. SCREEN_ORIENTATION_SENSOR:

* The Activity direction is determined by the physical direction sensor and displayed in the direction of the user's rotating device.

* ActivityInfo. SCREEN_ORIENTATION_NOSENSOR:

* Always ignore the orientation sensor's judgment. When the user rotates the device, the display does not rotate.

* ActivityInfo. SCREEN_ORIENTATION_SENSOR_LANDSCAPE:

* You want the Activity to be displayed on the horizontal screen, but you can change it according to the direction indicated by the Direction sensor.

* ActivityInfo. SCREEN_ORIENTATION_SENSOR_PORTRAIT:

* You want the Activity to be displayed on the portrait screen, but you can change it according to the direction indicated by the Direction sensor.

* ActivityInfo. SCREEN_ORIENTATION_REVERSE_LANDSCAPE:

* You want the Activity to be displayed on the horizontal screen, but it is opposite to the normal horizontal screen.

* ActivityInfo. SCREEN_ORIENTATION_REVERSE_PORTRAIT:

* You want the Activity to be displayed on the portrait screen, but it is opposite to the normal portrait screen.

* ActivityInfo. SCREEN_ORIENTATION_FULL_SENSOR:

* The Activity direction is determined by the Direction sensor. The display is rotated based on the movement of the user's device.

*/

FinalstaticintmOrientationValues [] = newint [] {

ActivityInfo. SCREEN_ORIENTATION_UNSPECIFIED,

ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE,

ActivityInfo. SCREEN_ORIENTATION_PORTRAIT,

ActivityInfo. SCREEN_ORIENTATION_USER,

ActivityInfo. SCREEN_ORIENTATION_BEHIND,

ActivityInfo. SCREEN_ORIENTATION_SENSOR,

ActivityInfo. SCREEN_ORIENTATION_NOSENSOR,

ActivityInfo. SCREEN_ORIENTATION_SENSOR_LANDSCAPE,

ActivityInfo. SCREEN_ORIENTATION_SENSOR_PORTRAIT,

ActivityInfo. SCREEN_ORIENTATION_REVERSE_LANDSCAPE,

ActivityInfo. SCREEN_ORIENTATION_REVERSE_PORTRAIT,

ActivityInfo. SCREEN_ORIENTATION_FULL_SENSOR,

};

/** Call this method when creating this Activity for the first time */

@ Override

Publicvoid onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

// Fill Layout

SetContentView (R. layout. screen_orientation );

// Query the drop-down control

MOrientation = (Spinner) findViewById (R. id. orientation );

// Reference array resources and create character array Adapter

ArrayAdapter <CharSequence> adapter = ArrayAdapter. createFromResource (this,

R. array. screen_orientations, android. R. layout. simple_spinner_item );

// Associate the array adapter with the drop-down control

MOrientation. setAdapter (adapter );

// Set the time listener for the drop-down control

MOrientation. setOnItemSelectedListener (

/**

* Select listener for the list project

* OnItemSelected: This callback is executed when a project is selected.

* OnNothingSelected: This callback is executed when no project is selected.

*/

New OnItemSelectedListener (){

Publicvoid onItemSelected (AdapterView <?> Parent, View view, int position, long id ){

// Set the Activity direction options

SetRequestedOrientation (mOrientationValues [position]);

}

Publicvoid onNothingSelected (AdapterView <?> Parent ){

// Unspecified direction

SetRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_UNSPECIFIED );

}

});

}

}


From FireOfStar's column

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.