Android改變手機(jī)屏幕朝向的方法
本文實(shí)例講述了Android改變手機(jī)屏幕朝向的方法。分享給大家供大家參考。具體如下:
模擬當(dāng)點(diǎn)擊按鈕時(shí),使手機(jī)朝向發(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="點(diǎn)擊更改屏幕朝向" />
<!-- android:hint: 當(dāng)文本為空時(shí)顯示該文本 -->
<EditText android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:hint="顯示當(dāng)前屏幕朝向" />
</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">
<!-- 設(shè)置手機(jī)的朝向,不然無法獲取手機(jī)的朝向
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" />
<!-- 改變手機(jī)配置權(quán)限 -->
<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{
//手機(jī)屏幕的朝向有7個(gè)可選值,分別如下
//SCREEN_ORIENTATION_BEHIND: 繼承Activity堆棧中當(dāng)前Activity下面的那個(gè)Activity的方向
//SCREEN_ORIENTATION_LANDSCAPE: 橫屏(風(fēng)景照) ,顯示時(shí)寬度大于高度
//SCREEN_ORIENTATION_PORTRAIT: 豎屏 (肖像照) , 顯示時(shí)高度大于寬度
//SCREEN_ORIENTATION_NOSENSOR: 忽略物理感應(yīng)器——即顯示方向與物理感應(yīng)器無關(guān),
//不管用戶如何旋轉(zhuǎn)設(shè)備顯示方向都不會(huì)隨著改變("unspecified"設(shè)置除外)
//SCREEN_ORIENTATION_SENSOR: 由物理感應(yīng)器決定顯示方向,它取決于用戶如何持有設(shè)備,
//當(dāng)設(shè)備被旋轉(zhuǎn)時(shí)方向會(huì)隨之變化——在橫屏與豎屏之間
//SCREEN_ORIENTATION_UNSPECIFIED: 未指定,此為默認(rèn)值,由Android系統(tǒng)自己選擇適當(dāng)?shù)姆较颍?
//選擇策略視具體設(shè)備的配置情況而定,因此不同的設(shè)備會(huì)有不同的方向選擇
//SCREEN_ORIENTATION_USER: 用戶當(dāng)前的首選方向
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ā)生改變時(shí)觸發(fā)
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
Toast.makeText(this, "系統(tǒng)的屏幕方向發(fā)生改變", Toast.LENGTH_LONG).show();
int o=getRequestedOrientation();//獲取手機(jī)的朝向
switch (o) {
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
editText.setText("當(dāng)前屏幕朝向?yàn)椋?橫屏");
break;
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
editText.setText("當(dāng)前屏幕朝向?yàn)椋?豎屏");
break;
}
//不能省略,否則會(huì)報(bào)android.app.SuperNotCalledException: Activity OrientationActivity did not
//call through to super.onConfigurationChanged()異常
super.onConfigurationChanged(newConfig);
}
}
運(yùn)行結(jié)果:

希望本文所述對大家的Android程序設(shè)計(jì)有所幫助。
- Android編程實(shí)現(xiàn)屏幕自適應(yīng)方向尺寸與分辨率的方法
- Android編程之分辨率處理相關(guān)代碼段合集
- android實(shí)用工具類分享(獲取內(nèi)存/檢查網(wǎng)絡(luò)/屏幕高度/手機(jī)分辨率)
- Android加載大分辨率圖片到手機(jī)內(nèi)存中的實(shí)例方法
- Android基礎(chǔ)之使用Fragment適應(yīng)不同屏幕和分辨率(分享)
- Android的單位以及屏幕分辨率詳解
- android根據(jù)分辨率自動(dòng)調(diào)整字體大小的實(shí)例代碼
- android開發(fā)中獲取手機(jī)分辨率大小的方法
- android計(jì)算pad或手機(jī)的分辨率/像素/密度/屏幕尺寸/DPI值的方法
- Android中獲取手機(jī)屏幕大小的方法
- Android編程實(shí)現(xiàn)獲得手機(jī)屏幕真實(shí)寬高的方法
- Android編程獲取手機(jī)屏幕分辨率大小的方法
相關(guān)文章
Android控件RadioButton實(shí)現(xiàn)多選一功能
這篇文章主要為大家詳細(xì)介紹了Android控件RadioButton實(shí)現(xiàn)多選一功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android UI 之實(shí)現(xiàn)多級樹形列表TreeView示例
這篇文章主要介紹了Android UI 之實(shí)現(xiàn)多級列表TreeView示例,TreeView就是在Windows中常見的多級列表樹,有興趣的可以了解一下。2017-03-03
Android平臺(tái)生成二維碼并實(shí)現(xiàn)掃描 & 識(shí)別功能
這篇文章主要介紹了Android平臺(tái)生成二維碼并實(shí)現(xiàn)掃描 & 識(shí)別功能的相關(guān)資料,需要的朋友可以參考下2016-06-06
ExpandableListView實(shí)現(xiàn)簡單二級列表
這篇文章主要為大家詳細(xì)介紹了ExpandableListView實(shí)現(xiàn)簡單二級列表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android開屏頁倒計(jì)時(shí)功能實(shí)現(xiàn)的詳細(xì)教程
本篇文章主要介紹了Android實(shí)現(xiàn)開屏頁倒計(jì)時(shí)功能實(shí)現(xiàn)的詳細(xì)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android開發(fā)中優(yōu)秀的app 異常處理機(jī)制
這篇文章主要介紹了Android開發(fā)中優(yōu)秀的app 異常處理機(jī)制 的相關(guān)資料,需要的朋友可以參考下2015-12-12
Android開發(fā)基礎(chǔ)實(shí)現(xiàn)最簡單的視頻播放示例
這篇文章主要為大家介紹了Android開發(fā)基礎(chǔ)實(shí)現(xiàn)最簡單的視頻播放示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

