Android螢幕大小相關技巧應用指南

來源:互聯網
上載者:User

Android應用程式中螢幕大小的設定大家應該都比較清楚,不過如何才能讓螢幕自己適應環境而改變大小呢?在這裡我們就可以為大家詳細介紹一下有關Android螢幕大小的自適應方式,協助大家理解。

不同的Android target會有不同的大小,應用程式的介面需要針對不同的大小調整介面元素的尺寸。而且Android螢幕大小也可以在橫屏和豎屏之間切換,介面也需要調整。

如何取得螢幕的方向:

預設情況下,當螢幕方面切換時,activity的onCreate()方法會被重新調用,所以可以在其中通過以下代碼來讀取屏的方向:

 
  1. view plaincopy to clipboardprint?  
  2. public void onCreate() {   
  3. if(this.getResources().getConfiguration()
    .orientation == Configuration.ORIENTATION_LANDSCAPE) {   
  4. Log.i("info", "landscape");   
  5. } else if (this.getResources().getConfiguration()
    .orientation == Configuration.ORIENTATION_PORTRAIT) {   
  6. Log.i("info", "portrait");   
  7. }   
  8. }   
  9. public void onCreate() {  
  10. if(this.getResources().getConfiguration()
    .orientation == Configuration.ORIENTATION_LANDSCAPE) {  
  11. Log.i("info", "landscape");  
  12. } else if (this.getResources().getConfiguration()
    .orientation == Configuration.ORIENTATION_PORTRAIT) {  
  13. Log.i("info", "portrait");  
  14. }  

如果在androidmanifest.xml中加入配置

 
  1. android:configChanges="orientation|keyboardHidden|navigation 

當螢幕翻轉時,Activity就不會重複的調用onCreate()、onPause()和onResume().

而是調用onConfigurationChanged(Configuration newConfig)

如何取得Android螢幕大小:

 
  1. view plaincopy to clipboardprint?  
  2. int screenWidth,screenHeight;   
  3. WindowManager windowManager = getWindowManager();   
  4. Display display = windowManager.getDefaultDisplay();   
  5. screenWidth = display.getWidth();   
  6. screenHeight = display.getHeight();   
  7. int screenWidth,screenHeight;  
  8. WindowManager windowManager = getWindowManager();  
  9. Display display = windowManager.getDefaultDisplay();  
  10. screenWidth = display.getWidth();  
  11. screenHeight = display.getHeight();  

也有人提到另一種Android螢幕大小的更改方法:

 
  1. view plaincopy to clipboardprint?  
  2. DisplayMetrics dm = new DisplayMetrics();   
  3. getWindowManager().getDefaultDisplay().getMetrics(dm);   
  4. int screenWidth = dm.widthPixels;   
  5. int screenHeight = dm.heightPixels;  

聯繫我們

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