2013-11-21 25 views
-1

我希望我的英文不錯。 我花了一整天的時間尋找解決方案,現在我想與大家分享:在EditText和TextView中自動調整文字android

for (int i=3;i<500;i++){ 

    String finalVal ="0"; //I used a number but is the same for chart 
    Paint paint = new Paint(); 
    paint.setTextSize(i); 
    paint.setTypeface(Typeface.SANS_SERIF); //if you use a particular font 
    paint.setColor(Color.BLACK); 
    paint.setStyle(Paint.Style.FILL); 

    Rect result = new Rect(); 
    paint.getTextBounds(finalVal, 0, finalVal.length(), result); 

    Resources r = getResources(); 
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, result.height(), r.getDisplayMetrics()); 

    if (px>et_no_perc.getHeight()){ 
     EditText.setTextSize(TypedValue.COMPLEX_UNIT_PX ,i-1); 
    break;  
    } 
} 

我希望它是有用的!

+0

我在編輯中從標題中刪除(解決)。發佈或接受答案表示已解決。其中一個是給出的,所以如果它確實解決了這個問題,那就考慮接受它。具體方法如下:訪問http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work,然後返回給出的答案,併爲它做同樣的事情,以及任何其他問題解決了。這告訴大家,問題已經解決,現在已經正式結束。 –

回答

-1
 <EditText 
     android:id="@+id/send_message_text" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="3dp" 
     android:hint="@string/type_text_message" 
     android:inputType="textMultiLine" 

     android:maxLines="10" 
     android:minLines="1" 
     android:scrollbars="vertical" 
     android:gravity="top|left" 
     android:layout_weight="1" 
     /> 
+1

這將有助於添加一些評論或解釋你的答案,而不是隻發佈代碼。 – skrrgwasme

0

TextViewEditText自動調整大小的文本是在現在的Android 8.0(API級26)的標準。看官方documentation

相關問題