仅为特定屏幕尺寸的Android设备应用一段代码的最佳方法是什么。
示例:
TextView textView = (TextView) rowView.findViewById(R.id.article_title_text);
// I would like to apply this code for only device Galaxy S3 mini (4" size)
textView.setText(Tools.cutTitleHomeForGalaxyMini(article.getTitle()));
// Else, i want to apply this code
textView.setText(Tools.cutTitleHome(article.getTitle()));
答案 0 :(得分:2)
这样你可以获得屏幕宽度,然后将这个屏幕宽度与galaxy mini进行比较,然后设置它
Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
答案 1 :(得分:1)
如果您有关于型号和屏幕尺寸的具体信息(正如您所提到的那样是Galaxy S3 mini(4“尺寸)),那么从系统获取设备的信息以检查并应用您的代码:
Build.MODEL, Build.PRODUCT, Build.MANUFACTURER
屏幕尺寸:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
希望这些帮助。
答案 2 :(得分:1)
您可以从像素屏幕宽度和高度以及像素密度计算屏幕尺寸,如下所示:
DisplayMetrics dm =new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
float height = dm.heightPixels/dm.xdpi;
float width = dm.widthPixels/dm.ydpi;
// you need to get the diagonal size to compare it to the 4'' of the Galaxy S3 mini.
float diagonalInch = FloatMath.sqrt(height*height+width*width);
然后使用简单的测试:
if (diagonalInch < 4){
...
}