gpt4 book ai didi

java - 如何根据屏幕尺寸启动 Activity ?

转载 作者:行者123 更新时间:2023-12-02 07:06:31 25 4
gpt4 key购买 nike

因此,我检查我的 main_activity 类,如果我的屏幕尺寸是 mdpi 或 hdpi,并根据情况我需要在游戏 Activity 中启动适当的方法。我的数据库中有两个表,其中包含 mdpi 和 hdpi 图像。但我什么也没得到。只有我的空白主要 Activity 。有什么问题?这是我的主要 Activity :

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);
}

}

最佳答案

按如下方式使用开关

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

关于java - 如何根据屏幕尺寸启动 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16040963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com