我是Android的新手。我喜欢在任何地方自由绘制对象,因此一直在使用绝对布局。但是我收到了一条消息,建议我使用其他布局。我已经了解到这是因为不同手机的分辨率不同。我的问题是,不使用绝对布局的唯一原因是这个吗?我已经编写了一个使用度量衡来调整像素的方法。
public int widthRatio(double ratioIn){
DisplayMetrics dm = new DisplayMetrics(); //gets screen properties
getWindowManager().getDefaultDisplay().getMetrics(dm);
double screenWidth = dm.widthPixels; //gets screen height
double ratio = screenWidth/100; //gets the ratio in terms of %
int displayWidth = (int)(ratio*ratioIn); //multiplies ratio desired % of screen
return displayWidth;
}
//method to get height or Ypos that is a one size fits all
public int heightRatio(double ratioIn){
DisplayMetrics dm = new DisplayMetrics(); //gets screen properties
getWindowManager().getDefaultDisplay().getMetrics(dm);
double screenHeight = dm.heightPixels; //gets screen height
double ratio = screenHeight/100; //gets the ratio in terms of %
int newHeight = (int)(ratio*ratioIn); //multiplies ratio by desired % of screen
return newHeight;
}
//sets size of any view (button, text, ...) to a one size fits all screens
public void setSizeByRatio(View object, int width, int height){
LayoutParams params = object.getLayoutParams();
params.width = widthRatio(width);
params.height = heightRatio(height);
}
如果我说setSizeByRatio(Button, 10, 25);,它将把按钮的宽度设置为屏幕宽度的10%,高度设置为屏幕高度的25%。
有没有任何手机不支持绝对布局?这种布局是否会引起其他问题?