【发布时间】:2018-04-22 21:19:20
【问题描述】:
我的客户希望在他的应用程序中内置一个标尺,以便在不携带其他工具的情况下测量某些东西。如果我在布置标尺时使用 in 单位,它适用于我的大多数测试设备。 Android Studio 警告我应该使用 dp 而不是 in,因为某些设备会显示不准确英寸单位。当我在我的 Amazon Fire 7 英寸平板电脑上进行测试时,哈希标记间隔太多。我的理解是 160dp 应该始终等于 1 英寸,所以我尝试分别为我的 1 英寸和 2 英寸哈希标记切换到 160dp 和 320dp。这一次,哈希标记靠得太近了。请参阅下面的屏幕截图。黑色标记使用英寸;红色标记正在使用 dp。正确的大小位于黑色和红色标记之间。
如何在所有设备上指定或转换为正确的英寸值?我更愿意在 XML 中指定所有值,但我非常愿意根据需要使用 Java 代码来调整布局。另外,是否有一种编程方式来发现英寸单位在给定设备上是否正确?
感谢您的帮助。
这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/one_inch_left"
android:layout_width="@dimen/ruler_hash_width"
android:layout_height="1px"
android:layout_marginTop="1in"
android:background="@color/solid_black"
app:layout_constraintTop_toBottomOf="parent"/>
<View
android:id="@+id/two_inch_left"
android:layout_width="@dimen/ruler_hash_width"
android:layout_height="1px"
android:layout_marginTop="2in"
android:background="@color/solid_black"
app:layout_constraintTop_toBottomOf="parent"/>
<View
android:id="@+id/one_inch_left2"
android:layout_width="@dimen/ruler_hash_width"
android:layout_height="1px"
android:layout_marginTop="160dp"
android:background="@color/red_button_color"
app:layout_constraintTop_toBottomOf="parent"/>
<View
android:id="@+id/two_inch_left2"
android:layout_width="@dimen/ruler_hash_width"
android:layout_height="1px"
android:layout_marginTop="320dp"
android:background="@color/red_button_color"
app:layout_constraintTop_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
【问题讨论】:
标签: android android-layout units-of-measurement