如何根据屏幕尺寸开始活动?

时间:2013-04-16 15:25:15

标签: java android sqlite

所以,我检查我的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);
            }

    }

3 个答案:

答案 0 :(得分:1)

似乎可以解决这个问题,你可以使用Configuration.screenLayout位掩码。

示例:

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

}

答案 1 :(得分: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;
}

如果您的应用只能获取MDPI,HDPI,XHDPI记得将屏幕兼容性放在您的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

答案 2 :(得分:0)

而不是它你可以直接把你的图像放在 res-&gt; mdpi,hdpi,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).