2017-11-09 7 views
0

私のクライアントは、別のツールを持ち歩くことなく特定のものを測定できるように、アプリ内にビルトインのルーラーが必要です。定規をレイアウトするとき単位で使用すると、ほとんどのテストデバイスで問題なく動作します。一部のデバイスは、インチ単位で不正確に表示されますので、そのDPは、にの代わりに使用されなければならないのにAndroidのメーカーは私を警告します。 Amazon Fire 7インチタブレットでテストすると、ハッシュマークの間隔が広すぎます。私の理解では、160dpは常に1インチに等しくなければならないので、私はそれぞれ1インチと2インチのハッシュマークに対して160dpと320dpに切り替えようとします。今回は、ハッシュマークがあまりにも近くにあります。下のスクリーンショットを参照してください。黒いマークはインチを使用しています。赤いマークはdpを使用しています。正しいサイズはの間にあり、黒と赤のマークです。一部のタブレットでインチ/ 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> 

enter image description here

答えて

0

私はXMLでこれを解決するためにどのような方法があります信じていません。 1つのdpは、"約"と160dpiで1ピクセルとなっていますが、これは厳しい保証ではありません。

しかし、DisplayMetricsクラスは、各方向にあなたのデバイスの実際の密度を与えるxdpiydpiフィールドを含んでいます。これらの値を使用してプログラムでルーラーを描画できます。

関連する問題