2012-03-15 139 views
1

我是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一样新。

+0

对于这个问题,检查答案:http://stackoverflow.com/questions/3441475/android-change-absolute-position-of-a-view-programmatically 这可能会有帮助。 – Reena 2012-03-15 05:28:28

+0

嗯,进一步思考。我猜一切都必须在父母身上。即使那个父母是主布局。我仍然想知道如何设置x和y位置,而不必做高度和宽度。 – JBreezy901 2012-03-15 05:33:40

+0

@Reena谢谢你的回答我的问题。我想我需要在寻找时变得更好。我寻找了一段时间,并没有看到。 – JBreezy901 2012-03-15 05:44:11

回答

1

这并不真正相关,但它清理了一行代码(如果你有更多这样的事情,可以用作清理更多代码行的参考)。

通过这种方式,DisplayMetrics适用于它们两个,而不是每次获得另一个轴时都必须输入它。

//method to get width or Xpos that is a one size fits all 
DisplayMetrics dm = new DisplayMetrics() { // This line now applies to both int and gets screen properties 
public int widthRatio(double ratioIn){ 
    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){ 
    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; 
    } 
} 
+0

感谢它是有用的,我很高兴有人看着我的代码objectivly。我需要所有的帮助,我可以得到哈哈。我原本以为这样做,但我的老师总是做大量的全局声明变量。我想它只是卡住了。我希望这些方法能够独立于外部变量之外,因此我可以将它们复制并粘贴到新项目中。但我会称之为很多,最好是创造一个新的扔掉它并创建一个新的一遍又一遍 – JBreezy901 2012-03-15 05:42:30