0

アプリでさまざまな画面サイズをサポートしたい。/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 (大) の場合、機能しませんでした。

助けはありますか?

4

5 に答える 5

2

サイズ: small、normal、large
密度: ldpi、mdpi、hdpi、
nodpi(オートスケールなし) アスペクト比: long、notlong
向き: land

使用法:

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>

コード レベルの微調整の場合:

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

そして忘れないでください:

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

Androidでの複数画面のサポートも参照してください

于 2013-01-08T13:22:57.530 に答える
1

レイアウト名は同じである必要があります

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

これを処理する必要はありません。Androidは使用するレイアウトを自動的に決定します

setContentView(R.layout.main1);
于 2013-01-08T13:16:47.350 に答える
1

これを見てください:

<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で異なる画面サイズをサポートする方法

于 2013-01-08T13:19:02.633 に答える
0

Android が自動的にこれを行います。さまざまな画面サイズに対して、さまざまな xml ファイルを作成しますが、それらに同じ名前をmain.xml付けlargeます。/res/layout-largesmall/res/layout-small

onCreate()、ちょうど入れますsetContentView(R.layout.main)

詳細については、Android Developers のこのサイトを読むことを検討してください。

于 2013-01-08T13:17:44.070 に答える