安卓:AbsoluteLayout? [英] Android: AbsoluteLayout?

查看:140
本文介绍了安卓:AbsoluteLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid。我喜欢有免费的范围绘制在以往任何时候我想的对象。所以,我一直在使用绝对布局。我得到一个消息,说要使用不同的布局。我已阅读,这是因为不同的手机的不同资源的。我的问题是,这是不使用绝对布局的唯一原因?我已使用度量来调整像素的方法。

I am new to Android. I like having the free range of drawing objects where ever I want. So i have been using Absolute Layout. I get a message saying to use a different layout. And I have read that this is because of the different res of different phones. My question is, is this the only reason in not using Absolute Layout? I have made a method that uses metrics to adjust the pixels.

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(巴顿,10,25);它将设置按钮宽度到屏幕的宽度和高度,以百分比25%的屏幕的10%的

So if i say setSizeByRatio(Button, 10, 25); It will set the buttons width to 10% of the width of the screen and the height to 25% percent of the screen.

有没有手机,绝对布局不工作呢?这是否布局引起的任何其他问题?

Are there any phones that Absolute Layouts do not work on? Does this layout cause any other issues?

推荐答案

之所以AbsoluteLayout是德precated是因为你在的LinearLayout或做同样多的GridLayout的替代品。看来你正试图以绝对位置和像素数,即一般应避免由于与varoius屏幕大小和密度问题的方法来计算位置。

The reason why the AbsoluteLayout is deprecated is because you have alternatives in the LinearLayout or the GridLayout that does the same and more. It seems that you are trying to calculate positions based on absolute positions and number of pixels, an approach that should in general be avoided due to issues with varoius screen sizes and densities.

阅读@amiekuser提供的链接,并重点了解如何最好的做法是。一些提示创建图像LDPI,MDPI和HDPI文件夹,使用单位DPI(密度无关的像素),而不是原始像素以及如何测试使用模拟器多个屏幕大小和密度您的应用程序。

Read the link that @amiekuser provided and focus on the understanding how the best practice is. Some hints are creating images for ldpi, mdpi and hdpi folders, using the unit for dpi (density independent pixels) instead of raw pixels and how to test your app on multiple screen sizes and densities using the emulator.

编辑:

要设置的x和景色,你必须使用的LayoutParams y位置。请参阅<一href="http://stackoverflow.com/questions/3294590/set-the-absolute-position-of-a-view-in-android">this问题如何设置TOPMARGIN和LEFTMARGIN使用的LayoutParams视图。

To set the x- and y-position of a View you must use LayoutParams. See this question on how to to set the TopMargin and LeftMargin for a View using LayoutParams.

这篇关于安卓:AbsoluteLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆