2014-11-13 6 views
0

픽셀 단위로 문자열 길이 얻기 :나는 다음과 같이 보이는 레이아웃이

|User Name  |  | 
|[email protected] | info | 

을 그리고 나는 그것이 텍스트 뷰에 맞는 경우에만 이름 경우 사용자의 이름과 두 번째 이름을 설정해야합니다 이름과 두 번째 이름 현재 화면에 비해 너무 깁니다.

|Tom Smith  |  | 
|[email protected] | info | 

및 대한

"톰 VeryVeryVeryLongSurname": 내가 지정한 텍스트 크기와 이름의 크기를 얻을 수 및 텍스트 뷰의 폭과 비교 어떻게

|Tom   |  | 
|[email protected] | info | 

"톰 스미스"에 대한 예를 들어

?

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 

     <TextView 
      android:id="@+id/email" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/info" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="bottom"/> 
</LinearLayout> 
+0

http://stackoverflow.com/questions/3630086/how-to-get-string-width-on-android – Carnal

답변

0

TextPaint#getTextBounds 방법을 참조하십시오

Paint.html #의 measureText 더욱 당신을 위해 무엇을 찾고 일치하는 것 같다 TextPaintPaint 클래스에서 http://developer.android.com/reference/android/graphics/Paint.html#getTextBounds%28char[],%20int,%20int,%20android.graphics.Rect%29

및 기타. http://developer.android.com/reference/android/graphics/Paint.html#measureText%28java.lang.CharSequence,%20int,%20int%29

어쨌든 android.graphics.Paint 클래스를 살펴보십시오.

+0

감사합니다! 왼쪽 화면 테두리와 다음보기 사이의 공간 너비는 어떻게 얻을 수 있습니까? – AndroidDev

관련 문제