2017-07-18 82 views
0

我測試了仿真器和真實設備,但是您可以看到40 dp和40.2 dp之間沒有變化!我正在研究一個字體敏感度非常高的項目,問題在於android文本引擎的行爲。Android TextView小數尺寸問題

有什麼辦法可以防止這種情況發生?

請幫忙!

這是代碼:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="SAMPLE TEXT" 
      android:textSize="40dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="SAMPLE TEXT" 
      android:textSize="40.2dp" /> 

    </LinearLayout> 

結果:

+0

看到這個: https://stackoverflow.com/questions/23981260/should-use-sp-instead-of-dp-for-text-sizes – 2017-07-18 09:45:33

+0

DP而不是使它SP和檢查 –

回答

0

你應該總是使用字體的SP,因爲它尊重用戶的喜好

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="SAMPLE TEXT" 
     android:textSize="40sp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="SAMPLE TEXT" 
     android:textSize="40.2sp" /> 
+0

不幸的是沒有變化。問題是android文本引擎的行爲。 – seyfx

0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="SAMPLE TEXT" 
    android:textSize="40sp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="SAMPLE TEXT" 
    android:textSize="40.2sp" /> 

你可以看到這樣的區別(在android studio中你需要放大布局屏幕)。沒有android文本引擎的行爲問題。

enter image description here

+0

這個問題是Android文本引擎的行爲。我試過dp,sp,px。我注意到,當在android textview中使用setTextSize方法指定字體大小時,字體大小的小數是根據分辨率確定的,我通過使用getTextSize方法實現了這一點。有一種舍入機制。另見http://imgur.com/a/JpEps – seyfx