国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android改變手機(jī)屏幕朝向的方法

2020-04-11 11:25:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(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ī)的朝向,不然無(wú)法獲取手機(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)的朝向無(wú)法獲取", 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)器無(wú)關(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é)果:

希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 长兴县| 大冶市| 长汀县| 武宁县| 曲阳县| 天门市| 稷山县| 政和县| 全南县| 来安县| 金平| 罗甸县| 东兴市| 沿河| 方正县| 绥江县| 民和| 廉江市| 阜新市| 长丰县| 天祝| 瑞昌市| 涞水县| 富蕴县| 滦平县| 开平市| 平山县| 大厂| 屯昌县| 沈阳市| 高邑县| 濉溪县| 汨罗市| 霍山县| 商洛市| 宁武县| 柯坪县| 久治县| 武宣县| 铜川市| 新兴县|