欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2015年09月25日 11:43:54   作者:Ruthless  
這篇文章主要介紹了Android改變手機(jī)屏幕朝向的方法,涉及Android動(dòng)態(tài)更改手機(jī)屏幕朝向的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(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類(lèi):

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),
          //不管用戶(hù)如何旋轉(zhuǎn)設(shè)備顯示方向都不會(huì)隨著改變("unspecified"設(shè)置除外)
          //SCREEN_ORIENTATION_SENSOR: 由物理感應(yīng)器決定顯示方向,它取決于用戶(hù)如何持有設(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: 用戶(hù)當(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ì)有所幫助。

相關(guān)文章

最新評(píng)論