App/Activity/Screen Orientation

來源:互聯網
上載者:User

標籤:android   style   blog   color   io   os   ar   sp   檔案   

測試android螢幕方向的小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);                    }                });    }

 

App/Activity/Screen Orientation

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.