2013-04-16 38 views
0

所以,我检查我的main_activity类,如果我的屏幕大小是mdpi或hdpi,并取决于我需要在我的游戏活动中启动适当的方法。我的数据库中有两张表,包括mdpi和hdpi图像。但我什么也没得到。只有我空白的主要活动。有什么问题?这是我的主要活动:如何根据屏幕尺寸开始活动?

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

      Display display = getWindowManager().getDefaultDisplay(); 
      int width = display.getWidth(); 
      int height = display.getHeight(); 

      if((width>320) && (width<480)){ 
       Intent i = new Intent(MainActivity.this, GameDanska.class); 
       i.putExtra("myMethod", "nextQuestionMDPI"); 
       startActivity(i); 

      } 
      else if((width>480) && (width<720)){ 
       Intent i2 = new Intent(MainActivity.this, GameDanska.class); 
       i2.putExtra("myMethod", "nextQuestionHDPI"); 
       startActivity(i2); 
      } 

    } 
+0

检查通过打印您的宽度值 – stinepike

+0

我怀疑宽== 0。 – Patrick

+0

你有没有检查过你的屏幕真的是什么?你只允许宽度为321-> 479和481-> 719。这些都是非常怪异的决议。例如我的GNex有1280x720,因此完全落在你允许的范围之外。 –

回答

1

如下

Intent i = new Intent(this, GameDanska.class); 

switch (getResources().getDisplayMetrics().densityDpi) { 
    case DisplayMetrics.DENSITY_MEDIUM: 
     i.putExtra("myMethod", "nextQuestionMDPI"); 
     startActivity(i); 
     break; 
    default: 
     i.putExtra("myMethod", "nextQuestionHDPI"); 
     startActivity(i); 
     break; 
} 

如果您的应用只针对abailable MDPI,华电国际,XHDPI记得把屏幕compabilities你manifest.xml文件使用的开关。

<compatible-screens> 
    <screen android:screenSize=["small" | "normal" | "large" | "xlarge"] 
      android:screenDensity=["ldpi" | "mdpi" | "hdpi" | "xhdpi"] /> 
    ... 
</compatible-screens> 

你的情况,而不是将其

<compatible-screens> 
    <screen android:screenSize="small" android:screenDensity="mdpi"/> 
    <screen android:screenSize="small" android:screenDensity="hdpi"/> 
    <screen android:screenSize="small" android:screenDensity="xhdpi"/> 

    <screen android:screenSize="normal" android:screenDensity="mdpi"/> 
    <screen android:screenSize="normal" android:screenDensity="hdpi"/> 
    <screen android:screenSize="normal" android:screenDensity="xhdpi"/> 

    <screen android:screenSize="large" android:screenDensity="mdpi"/> 
    <screen android:screenSize="large" android:screenDensity="hdpi"/> 
    <screen android:screenSize="large" android:screenDensity="xhdpi"/> 

    <screen android:screenSize="xlarge" android:screenDensity="mdpi"/> 
    <screen android:screenSize="xlarge" android:screenDensity="hdpi"/> 
    <screen android:screenSize="xlarge" android:screenDensity="xhdpi"/> 
</compatible-screens> 

API Screen reference

Switch reference

+0

我将只使用mdpi和hdpi。我不认为任何人都有ldpi屏幕了,对于xhdpi我会使用hdpi图像。我在模拟器上试了一下,看起来不错。 – marjanbaz

+0

4月2日10.4%使用ldpi [http://developer.android.com/about/dashboards/index.html#Screens](http://developer.android.com/about/dashboards/index.html#Screens) – AlexBcn

+0

我更新了答案。你会认为它已经接受:)? – AlexBcn

1

似乎这样可以做到这一点,您可以使用Configuration.screenLayout位掩码。

例子:

if ((getResources().getConfiguration().screenLayout & 
    Configuration.SCREENLAYOUT_SIZE_MASK) == 
     Configuration.SCREENLAYOUT_SIZE_LARGE) { 
    // on a large screen device ... 

} 
0

,你可以直接把你的图片里面水库 - > MDPI,华电国际,xhdpi,LDPI文件夹

for ldpi 320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc)

for mdpi 480dp: a tweener tablet like the Streak (480x800 mdpi).

for hdpi 600dp: a 7” tablet (600x1024 mdpi).

for xhdpi 720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).