我是android新手。我开始尝试构建一个方法库。我在java中更加舒适,并且由于运行时的所有内容似乎都发生在java中,我正在尝试构建处理java中GUI的方法。到目前为止,我没有太多的东西,我正在寻找定义在x和y位置绘制对象的位置。这是我到目前为止有:Android:设置x和y位置
//method to get width or Xpos that is a one size fits all
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 heightByRatio(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){
ViewGroup.LayoutParams params = object.getLayoutParams(); // gets params of view
params.width = widthRatio(width); // sets width by portion of screen
params.height = heightByRatio(height); // sets height by portion of screen
}
所以,如果我有一个名为Button按钮,和我说 setSizeByRatio(按钮,25,50);它将按钮高度设置为屏幕的25%,并将其高度设置为任何屏幕的50%。
我的主要问题是你如何设置你想要它开始绘制的x和y位置,就像你在注册java中一样? 我跑过布局(l,t,r,b);但它只设置相对于父项的x和y。
下一个问题是什么,至于我应该学习的GUI方法?我知道这会杀死很多人,但我如何评论XML?对于XML来说,我和android一样新。
对于这个问题,检查答案:http://stackoverflow.com/questions/3441475/android-change-absolute-position-of-a-view-programmatically 这可能会有帮助。 – Reena 2012-03-15 05:28:28
嗯,进一步思考。我猜一切都必须在父母身上。即使那个父母是主布局。我仍然想知道如何设置x和y位置,而不必做高度和宽度。 – JBreezy901 2012-03-15 05:33:40
@Reena谢谢你的回答我的问题。我想我需要在寻找时变得更好。我寻找了一段时间,并没有看到。 – JBreezy901 2012-03-15 05:44:11