【发布时间】:2014-07-02 20:00:14
【问题描述】:
首先,我必须说我是初学者。但是已经制作了一个可以在我的 Nexus 7 平板电脑上运行的应用程序。但我想让它适用于所有设备。是的,我知道,这个问题以前就有过。但我想听听你的意见,如果等的话,它会起作用吗...
public void setLayout()
{
DisplayMetrics dm = new DisplayMetrics(); //avoid this it just gets screen size.
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
int dens=dm.densityDpi;
double wi=(double)width/(double)dens;
double hi=(double)height/(double)dens;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double ss = Math.sqrt(x+y);
if(ss <7) //n7 If screen size is nexus 7, application will set nexus7 layout
{
setContentView(R.layout.screen7);
}
else
{
setContentView(R.layour.default); //user is not using nexus 7 so we will set default layout
}
但我需要在每个需要设置布局的活动中运行此方法。就像在 MainActivity 中一样,而不是使用 setContentView(R.layout.activity_main);
我想使用 setLayout(); //so 方法检查用户正在使用的屏幕尺寸,以便我们可以显示可用的最佳布局。
我希望你明白我想要做什么;) 谢谢 :) 如果您需要 LogCat 错误,请告诉我。
【问题讨论】:
-
developer.android.com/guide/practices/screens_support.html 不要推出自己的解决方案。只需使用 Android 的处理方式...
-
@DerGolem,我不重复,我只是想做我自己的方法!!!