android實現簡單的橫豎屏之間的切換

來源:互聯網
上載者:User

標籤:android   style   http   io   ar   color   os   sp   java   

Configuration類 用於擷取手機上的裝置的配置資訊,包括使用者配置項和系統的動態設定項

可通過Activity的如下方法獲得:

Configuration cfg = getResource().getConfiguration();

屬性

public float fontScale; 擷取目前使用者設定的字型的縮放因子

 public int mcc; 擷取移動訊號的國家碼

 public int mnc;擷取移動訊號的網路碼

 public Locale locale;擷取使用者當前的Local

 public boolean userSetLocale;//Locale should persist on setting.  This is hidden because it is really  * questionable      whether this is the right way to expose the functionality.

public int keyboardHidden: 擷取當前裝置所關聯的鍵盤類型。

       KEYBOARD_NOKEYS、KEYBOARD_QWERTY(普通電腦鍵盤)、

       KEYBOARD_12KEY(只有12個鍵的小鍵盤)

public int keyboardHidden; 該屬性返回一個標誌當前鍵盤是否可用:(該屬性不僅會判斷系統的硬體鍵盤,還會判斷系統的軟體機碼盤(位於螢幕上的))

1.如果系統的硬體鍵盤不可用,但軟體機碼盤可用,會返回KEYBOARDHIDDEN_NO()、

2.只有當兩個鍵盤都不可用時,才返回KEYBOARDHODDEN_YES


public int orientation:擷取系統螢幕的方向,:ORIENTATION_LANDSPACE(橫屏)、ORIENTATION_PORTRAIT(豎向螢幕)、ORIENTATION_SQUARE(方形螢幕)等屬性

現在我們實現一個簡單的螢幕切換(通過點擊一個普通的button來實現):

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_marginTop="142dp"        android:text="點擊改變螢幕方向" /></RelativeLayout>

Main.java

public class Main extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Button b = (Button) findViewById(R.id.button1);b.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Configuration cfg = getResources().getConfiguration();if (cfg.orientation == Configuration.ORIENTATION_LANDSCAPE) {Main.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);}if (cfg.orientation == Configuration.ORIENTATION_PORTRAIT) {Main.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);}}});}/** * 在AndroidManifest.xml檔案中設定 * android:configChanges="orientation"  * 螢幕方向改變,會回調這個方法   */@Overridepublic void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);String str = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ? "橫屏顯示": "豎屏顯示";Toast.makeText(this, str, Toast.LENGTH_LONG).show();}}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.main"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="10"        android:targetSdkVersion="12" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <!-- 設定activity可以監聽到螢幕方向改變的事件  -->        <activity            android:name=".Main"            android:label="@string/app_name"            android:configChanges="orientation" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

! !!設定檔中的:

 android:targetSdkVersion="12"

應為12,設定得過高,onConfigurationChanged()方法不會被觸發。



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.