Android改變手機屏幕朝向的方法
本文實例講述了Android改變手機屏幕朝向的方法。分享給大家供大家參考。具體如下:
模擬當點擊按鈕時,使手機朝向發(fā)生改變。
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, "系統(tǒng)的朝向無法獲取", 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系統(tǒng)自己選擇適當?shù)姆较颍? //選擇策略視具體設備的配置情況而定,因此不同的設備會有不同的方向選擇 //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); } } } }); } /** * 配置信息發(fā)生改變時觸發(fā) */ @Override public void onConfigurationChanged(Configuration newConfig) { Toast.makeText(this, "系統(tǒng)的屏幕方向發(fā)生改變", 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程序設計有所幫助。
- Android編程實現(xiàn)屏幕自適應方向尺寸與分辨率的方法
- Android編程之分辨率處理相關代碼段合集
- android實用工具類分享(獲取內存/檢查網絡/屏幕高度/手機分辨率)
- Android加載大分辨率圖片到手機內存中的實例方法
- Android基礎之使用Fragment適應不同屏幕和分辨率(分享)
- Android的單位以及屏幕分辨率詳解
- android根據(jù)分辨率自動調整字體大小的實例代碼
- android開發(fā)中獲取手機分辨率大小的方法
- android計算pad或手機的分辨率/像素/密度/屏幕尺寸/DPI值的方法
- Android中獲取手機屏幕大小的方法
- Android編程實現(xiàn)獲得手機屏幕真實寬高的方法
- Android編程獲取手機屏幕分辨率大小的方法
相關文章
Android開發(fā)方式之Java+html+javascript混合開發(fā)
這篇文章主要為大家詳細介紹了Android開發(fā)方式的其中一種Java+html+javascript混合開發(fā),感興趣的小伙伴們可以參考一下2016-06-06丟失Android系統(tǒng)庫或者Conversion to Dalvik format failed with error
這篇文章主要介紹了丟失Android系統(tǒng)庫或者Conversion to Dalvik format failed with error 1錯誤的解決方法,分析了Android系統(tǒng)庫丟失及版本問題的處理技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12Android 數(shù)據(jù)存儲之 FileInputStream 工具類及FileInputStream類的使用
這篇文章主要介紹了Android 數(shù)據(jù)存儲之 FileInputStream 工具類及FileInputStream類的使用的相關資料,需要的朋友可以參考下2015-11-11Android編程實現(xiàn)WebView全屏播放的方法(附源碼)
這篇文章主要介紹了Android編程實現(xiàn)WebView全屏播放的方法,結合實例形式較為詳細的分析了Android實現(xiàn)WebView全屏播放的布局與功能相關技巧,需要的朋友可以參考下2015-11-11Android之RecycleView實現(xiàn)指定范圍的拖動效果
這篇文章主要介紹了Android之RecycleView實現(xiàn)指定范圍的拖動效果的實例代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07Android App在線程中創(chuàng)建handler的方法講解
這篇文章主要介紹了Android App在線程中創(chuàng)建handler的方法講解,文中同時講解了handler和線程的關系以及使用Handler時一些需要注意的地方,需要的朋友可以參考下2016-03-03Android開發(fā)之超實用的系統(tǒng)管理工具類【SD卡,網絡,uri,屏幕,網絡,軟鍵盤,文本,進程等】
這篇文章主要介紹了Android開發(fā)之超實用的系統(tǒng)管理工具類,涉及Android針對SD卡,網絡,uri,屏幕,網絡,軟鍵盤,文本,進程等相關操作技巧,需要的朋友可以參考下2018-02-02