android2.3 api demo 学习系列(14)--App/Activity/Screen Orientation

下面我们来学习下Screen Orientaiton的demo。

1、首先我们在values下面新建文件arrays.xml(用来在下拉列表中显示)

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <!-- Used in app/Screen Orientation -->
    <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>

 

 2、我们在代码中定义相对应数组

 

 final static int mOrientationValues[] = new int[] {
        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,
    };

 

 3、处理用户选择

 

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_activity_screen_orientation);
        
        mOrientation = (Spinner)findViewById(R.id.app_activity_screen_orientation);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.screen_orientations, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mOrientation.setAdapter(adapter);
        mOrientation.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {
                        setRequestedOrientation(mOrientationValues[position]);
                    }

                    public void onNothingSelected(AdapterView<?> parent) {
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
                    }
                });
    }

 效果图:


android2.3 api demo 学习系列(14)--App/Activity/Screen Orientation_第1张图片


android2.3 api demo 学习系列(14)--App/Activity/Screen Orientation_第2张图片

 

相关方法:

createFromResource()方法创建一个新的 ArrayAdapter, 定义了数组中得每一项如何显示在下来列表上.  setDropDownViewResource(int)定义了下拉列表展开时每一项如何显示.

AdapterView.OnItemSelectedListener 要求必须有 onItemSelected() 和onNothingSelected() 两个回调方法.根据其方面名我们就可以了解其用途。

 

 

public void setRequestedOrientation (int requestedOrientation)

改变activity的方向. 如果activity正在显示,则改变会立刻生效 (有可能导致activity重起). 否则改变会在activity下次可见的时候生效。

参数requestedOrientation可选值:

SCREEN_ORIENTATION_UNSPECIFIED,SCREEN_ORIENTATION_LANDSCAPESCREEN_ORIENTATION_PORTRAIT

SCREEN_ORIENTATION_USERSCREEN_ORIENTATION_BEHINDSCREEN_ORIENTATION_SENSOR,

SCREEN_ORIENTATION_NOSENSORSCREEN_ORIENTATION_SENSOR_LANDSCAPE

SCREEN_ORIENTATION_SENSOR_PORTRAITSCREEN_ORIENTATION_REVERSE_LANDSCAPE,

SCREEN_ORIENTATION_REVERSE_PORTRAITSCREEN_ORIENTATION_FULL_SENSOR.


.

 

 

你可能感兴趣的:(android,api,demo)