簡體   English   中英

在Android中支持不同的屏幕尺寸

[英]support for different screen sizes in android

我想我的應用程序支持不同的屏幕尺寸。 我在/ res目錄中添加文件夾“ layout-small和layout-large”。 但是無法在我的活動中訪問此文件夾中的XML。因此,我將所有XML添加到默認布局中並添加此代碼

if((getResources().getConfiguration().screenLayout && 
      Configuration.SCREENLAYOUT_SIZE_SMALL) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
          setContentView(R.layout.main1);
    }else if((getResources().getConfiguration().screenLayout &&
                Configuration.SCREENLAYOUT_SIZE_LARGE) == Configuration.SCREENLAYOUT_SIZE_LARGE){
                     setContentView(R.layout.main2);
        }
        else
            setContentView(R.layout.main);

在我的活動中,但是當我的AVD皮膚為1024 * 600且hw.lcd.dencity為160(大)時,它不起作用。

有什么幫助嗎?

尺寸:小,普通,大
密度:ldpi,mdpi,hdpi,
nodpi(無自動縮放)長寬比:長,不長
方向:土地

用法:

res/layout/my_layout.xml            
res/layout-small/my_layout.xml      
res/layout-large/my_layout.xml      
res/layout-large-long/my_layout.xml      
res/layout-large-land/my_layout.xml     
res/drawable-ldpi/my_icon.png  
res/drawable-mdpi/dpi/my_icon.png  
res/drawable-hdpi/my_icon.png      
res/drawable-nodpi/composite.xml   

將您的應用限制為特定的屏幕尺寸(通過AndroidManifest):

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
...
<supports-screens
          android:largeScreens="true"
          android:normalScreens="true"
          android:smallScreens="true"
          android:anyDensity="true" />
...
</manifest>

對於代碼級別的tweeking:

float scale = getContext().getResources().getDisplayMetrics().density;

並且不要忘記:

dpi    = 160; //At 160dpi
pixels = dips * (density / dpi)

另請參閱在Android中支持多屏

布局名稱必須相同

layout-small \ main1.xml
layout-normal\ main1.xml
layout-large \ main1.xml

您不需要處理此問題,Android會自動決定將使用哪種布局

setContentView(R.layout.main1);

請看這個:

<supports-screens android:smallScreens="true" 
      android:normalScreens="true" 
      android:largeScreens="true"
      android:xlargeScreens="true"
      android:anyDensity="true" />

res/layout/my_layout.xml         // layout for normal screen size ("default")
res/layout-small/my_layout.xml       // layout for small screen size
res/layout-large/my_layout.xml       // layout for large screen size
res/layout-xlarge/my_layout.xml      // layout for extra large screen size

res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

res/drawable-mdpi/my_icon.png        // bitmap for medium density
res/drawable-hdpi/my_icon.png        // bitmap for high density
res/drawable-xhdpi/my_icon.png       // bitmap for extra high density

更多閱讀,所以請看這個鏈接如何在Android中支持不同的屏幕尺寸

Android會自動為您執行此操作。 對於不同的屏幕尺寸,請制作不同的xml文件,但要給它們指定相同的名稱,例如main.xml然后將large文件放在/res/layout-large文件夾中,將small放在/res/layout-small等文件夾中。等等

onCreate() ,只需將setContentView(R.layout.main)

有關更多信息,請考慮從Android Developers閱讀本網站

Android將自動為給定的特定設備選擇最佳資源。如果沒有可用的匹配資源,系統將使用默認資源並根據需要將其放大或縮小以匹配當前屏幕尺寸和密度。

“默認”資源是那些沒有用配置限定符標記的資源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM