所有屏幕上的视图大小相同

时间:2017-01-12 12:16:17

标签: android size density-independent-pixel

我需要以毫米为单位显示精确尺寸的视图。我面临的问题是,在我应用视图大小im mm时,在许多Android设备上,输出不同。有没有办法在所有屏幕上实现完全相同的视图大小? 我尝试过:

  • pixels =(mm * dpi)/ 25.4
  • context.getResources()。getDimensionPixelOffset(R.dimen.size)(100mm)
  • TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM,100,displayMetrics);

什么都没有

1 个答案:

答案 0 :(得分:2)

您必须专注于所有屏幕的设备兼容性:

 add this dependency to your build.gradle
 compile 'com.intuit.sdp:sdp-android:1.0.3'

 for eg : dimens.xml 


<dimen name="_1sdp">1.20dp</dimen>
<dimen name="_2sdp">2.40dp</dimen>
<dimen name="_3sdp">3.60dp</dimen>
<dimen name="_4sdp">4.80dp</dimen>
<dimen name="_5sdp">6.00dp</dimen>
<dimen name="_6sdp">7.20dp</dimen>
<dimen name="_7sdp">8.40dp</dimen>
<dimen name="_8sdp">9.60dp</dimen>
<dimen name="_9sdp">10.80dp</dimen>
<dimen name="_10sdp">12.00dp</dimen>
<dimen name="_11sdp">13.20dp</dimen>
<dimen name="_12sdp">14.40dp</dimen>
<dimen name="_13sdp">15.60dp</dimen>
<dimen name="_14sdp">16.80dp</dimen>
<dimen name="_15sdp">18.00dp</dimen>
<dimen name="_16sdp">19.20dp</dimen>
<dimen name="_17sdp">20.40dp</dimen>
<dimen name="_18sdp">21.60dp</dimen>
<dimen name="_19sdp">22.80dp</dimen>
<dimen name="_20sdp">24.00dp</dimen>
<dimen name="_21sdp">25.20dp</dimen>
<dimen name="_28sdp">33.60dp</dimen>
<dimen name="_30sdp">36.00dp</dimen>
<dimen name="_31sdp">37.20dp</dimen>
<dimen name="_32sdp">38.40dp</dimen>
<dimen name="_33sdp">39.60dp</dimen>
<dimen name="_34sdp">40.80dp</dimen>
<dimen name="_40sdp">48.00dp</dimen>
<dimen name="_41sdp">49.20dp</dimen>
<dimen name="_42sdp">50.40dp</dimen>

    for eg:  <TextView
                android:layout_width="match_parent"
                android:layout_height="@dimen/_10sdp"
                android:layout_gravity="center"
                android:gravity="center"/>
     will support all screen sizes.