Android configChanges屬性

來源:互聯網
上載者:User

android:configChanges="keyboardHidden|orientation"配置,當然還是很有用的。
  就是如果配置了這個屬性,當我們橫豎屏切換的時候會直接調用onCreate方法中的onConfigurationChanged方法,而不會重新執行onCreate方法,那當然如果不配置這個屬性的話就會重新調用onCreate方法了,下面是測試

  AndroidManifest.xml檔案

  <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.test"
        android:versionCode="1"
        android:versionName="1.0">
      <uses-sdk android:minSdkVersion="8" />

      <application android:icon="@drawable/icon" android:label="@string/app_name">
          <activity android:name=".TestActivity"
                    android:label="@string/app_name"
                    android:configChanges="keyboardHidden|orientation">
              <intent-filter>
                  <action android:name="android.intent.action.MAIN" />
                  <category android:name="android.intent.category.LAUNCHER" />
              </intent-filter>
          </activity>

      </application>
  </manifest>

  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"
      >
      <TextView  
          android:id="@+id/tv"
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:text="橫豎屏切換測試"
          />
      <EditText 
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:id="@+id/et"
          />
  </LinearLayout>

  TestActivity.java檔案

  package com.test;

  import android.app.Activity;
  import android.content.res.Configuration;
  import android.os.Bundle;
  import android.widget.EditText;
  import android.widget.TextView;

  public class TestActivity extends Activity {
      EditText et;
      TextView tv;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          et = (EditText) findViewById(R.id.et);
          tv = (TextView) findViewById(R.id.tv);
          System.out.println("我是onCreate方法");
      }
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
          if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
              tv.setText("橫屏");
          }else{
              tv.setText("豎屏");
          }
          
      }
  } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.