Android應用程式資源

來源:互聯網
上載者:User

引用樣式屬性

?[<package_name>:][<resource_type>/]<resource_name>  

  應用當前主題下的指定屬性值
    <EditText id="text"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:textColor="?android:textColorSecondary"
      android:text="@string/hello_world" />

捕獲運行時配置變化

  螢幕方向、鍵盤有效性和語言等配置的變化通常引起Android重啟正在啟動並執行Activity(OnDestroy被調用,緊接著調用OnCreate)。
一、通過以下方法儲存資料

    //onRetainNonConfigurationInstance被調用在OnStop和OnDestory之間

    //MyDataObject對象不能保有任何與Context相關聯的資料(Drawable、Adapter、View等),以防止記憶體泄露

    //不可用Bundle,Bundle在儲存大資料量狀態時需要序列化與還原序列化,會消費很多記憶體並可能會是配置的變化過程變慢
    @Override
    public Object onRetainNonConfigurationInstance() {
      final MyDataObject data = collectMyLoadedData();
      return data;
    }

 

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
      if (data == null) {
        data = loadMyData();
      }
      ...
    }
   
二、也可以聲明由自己捕獲運行時配置變化,以避免Android重啟Activity
    <activity android:name=".MyActivity"
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name">
  當上述聲明的運行時配置發生變化時,將會接收到onConfigurationChanged事件

    //當未聲明的運行時配置發生變化時,Android依然會重啟正在啟動並執行Activity
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);

      // Checks the orientation of the screen
      if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
      } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
      }
      // Checks whether a hardware keyboard is available
      if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
        Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
      } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
        Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
      }
    }

相關文章

聯繫我們

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