【问题标题】:one size fits all screen resolution一种尺寸适合所有屏幕分辨率
【发布时间】:2012-03-14 07:59:47
【问题描述】:

我对 Android 非常陌生。像不到一周的新。我想将所有内容调整为如果它占用了一部手机屏幕的四分之一,我希望它占据所有手机屏幕的 1/4。我一直在玩弄自己的方法,但我不喜欢它,必须有更好的方法。这就是我想出的。

public int displayWidth(double ratioIn){
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double resolutionWidth = dm.widthPixels; //gets the width in pixels
    double ratio = (resolutionWidth/100); //devides by 100 so that ratio will be in terms of %
    int displayWidth = (int)(ratio*ratioIn); 
    return displayWidth;
}

问题是精度有所下降(尽管我已经尝试过 480 分辨率并且几乎完美)。有没有更好的方法呢?我尝试过使用 dp。它有时会接近,但还不够接近。

【问题讨论】:

  • 您可以为您的问题添加一些更有用的标签。像安卓。 编辑: 我不是 android 专家,但是没有类似于 javas 布局管理器的东西吗?或者你不使用一些 xml 来定义你的 GUI?
  • 我的错,我以为我是在 android 部分问的。我使用 xml 来定义 GUI。我是 android 新手,但是这个和 reg java 编程的区别在于计算机上只有这么多的显示模式。手机似乎有更多。

标签: android size resolution


【解决方案1】:

如果您可以在 XML 中执行此操作,您可以使用带有属性 layout_weight 的 LinearLayouts。如果您希望您的项目占屏幕的 3/4,只需为其赋予 3 的权重并添加一个权重为 1 的空视图。但请注意 - 这会占用大量性能。

【讨论】:

  • 我还应该提到我是 XML 新手。我不知道重量和重力。我一直在玩它,但它只占用了我宽度的一半而不是高度。假设我希望它在屏幕宽度的一半处绘制一个按钮,这种方法不会这样做。这就是我所拥有的。对吗?
  • ` schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent " android:orientation="vertical" > `
  • 这应该看起来像这样: schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizo​​ntal" >
猜你喜欢
  • 2015-12-21
  • 2011-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多