【问题标题】:What units to pass UI methods?传递 UI 方法的单位是什么?
【发布时间】:2011-08-04 04:17:17
【问题描述】:

阅读 Android 开发者指南的 Supporting Multiple Screens,它说最好使用与密度无关的像素 (dp) 而不是像素,它还说 Android 可以处理不同密度设备上的大部分应用程序渲染。

现在我的问题是,如果我们看一下 View 类的填充函数

setPadding (int left, int top, int 对,int 底部)

left     the left padding in pixels
top       the top padding in pixels
right   the right padding in pixels
bottom    the bottom padding in pixels

当我使用这个函数时,是否可以直接传递值,或者我必须传递从 dp 到 px 的转换值,以便在所有设备中最好地显示 UI?

【问题讨论】:

    标签: android


    【解决方案1】:

    查看this padding reference 并搜索 android:padding

    看起来您需要将倾角转换为像素才能设置填充。您应该将值从 dip 转换为像素,以便它在不同的设备上正常工作。

    【讨论】:

    • 谢谢。我也是这么想。实际上我只检查了 setPadding 函数描述并错过了从文档中读取 android:padding 描述。根据它的描述,似乎我们必须将 dp 转换为像素才能使用 setPadding 功能。
    【解决方案2】:

    将倾角值转换为像素并用它填充。

    final float scale = getContext().getResources().getDisplayMetrics().density;
    
    int valuePixels = (int)(valueDip * scale);
    

    【讨论】:

      猜你喜欢
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      相关资源
      最近更新 更多