Android改變手機螢幕朝向的方法_Android

來源:互聯網
上載者:User

本文執行個體講述了Android改變手機螢幕朝向的方法。分享給大家供大家參考。具體如下:

類比當點擊按鈕時,使手機朝向發生改變。

main.xml布局檔案:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical" android:layout_width="fill_parent"  android:layout_height="fill_parent">  <Button android:id="@+id/btn"     android:layout_width="fill_parent"    android:layout_height="wrap_content"     android:text="點擊更改螢幕朝向" />  <!-- android:hint: 當文本為空白時顯示該文本 -->  <EditText android:id="@+id/editText"     android:layout_width="fill_parent"    android:layout_height="wrap_content"     android:cursorVisible="false"    android:hint="顯示當前螢幕朝向" /></LinearLayout>

資訊清單檔:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"   package="com.ljq.activity"   android:versionCode="1"   android:versionName="1.0">  <!-- 設定手機的朝向,不然無法擷取手機的朝向     android:screenOrientation="portrait"     android:configChanges="orientation" -->  <application android:icon="@drawable/icon" android:label="@string/app_name">    <activity android:name=".OrientationActivity"         android:label="@string/app_name"         android:screenOrientation="portrait"         android:configChanges="orientation">      <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />      </intent-filter>    </activity>  </application>  <uses-sdk android:minSdkVersion="7" />  <!-- 改變手機配置許可權 -->  <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /></manifest>

OrientationActivity類:

package com.ljq.activity;import android.app.Activity;import android.content.pm.ActivityInfo;import android.content.res.Configuration;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class OrientationActivity extends Activity {  private EditText editText=null;  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    editText=(EditText)findViewById(R.id.editText);    Button btn=(Button)findViewById(R.id.btn);    btn.setOnClickListener(new View.OnClickListener(){      public void onClick(View v) {        //判斷是否可以獲得requestedOrientation屬性        if(OrientationActivity.this.getRequestedOrientation()==-1){          Toast.makeText(OrientationActivity.this, "系統的朝向無法擷取", Toast.LENGTH_LONG).show();        }else{          //手機螢幕的朝向有7個可選值,分別如下          //SCREEN_ORIENTATION_BEHIND: 繼承Activity堆棧中當前Activity下面的那個Activity的方向          //SCREEN_ORIENTATION_LANDSCAPE: 橫屏(風景照) ,顯示時寬度大於高度           //SCREEN_ORIENTATION_PORTRAIT: 豎屏 (肖像照) , 顯示時高度大於寬度           //SCREEN_ORIENTATION_NOSENSOR: 忽略物理感應器——即顯示方向與物理感應器無關,          //不管使用者如何旋轉裝置顯示方向都不會隨著改變("unspecified"設定除外)          //SCREEN_ORIENTATION_SENSOR: 由物理感應器決定顯示方向,它取決於使用者如何持有裝置,          //當裝置被旋轉時方向會隨之變化——在橫屏與豎屏之間          //SCREEN_ORIENTATION_UNSPECIFIED: 未指定,此為預設值,由Android系統自己選擇適當的方向,          //選擇策略視具體裝置的配置情況而定,因此不同的裝置會有不同的方向選擇          //SCREEN_ORIENTATION_USER: 使用者當前的首選方向          if(OrientationActivity.this.getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){            OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);          }else if(OrientationActivity.this.getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){            OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);          }        }      }    });  }  /**   * 配置資訊發生改變時觸發   */  @Override  public void onConfigurationChanged(Configuration newConfig) {    Toast.makeText(this, "系統的螢幕方向發生改變", Toast.LENGTH_LONG).show();    int o=getRequestedOrientation();//擷取手機的朝向    switch (o) {    case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:      editText.setText("當前螢幕朝向為: 橫屏");      break;    case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:      editText.setText("當前螢幕朝向為: 豎屏");      break;    }    //不能省略,否則會報android.app.SuperNotCalledException: Activity OrientationActivity did not    //call through to super.onConfigurationChanged()異常    super.onConfigurationChanged(newConfig);  }}

運行結果:

希望本文所述對大家的Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.