2016-11-09 11 views
0

코드에서 나중에 사용하기 위해 텍스트 뷰의 텍스트 크기를 가져 오려고합니다.안드로이드 - 레이아웃에 맞게 텍스트 뷰 자동 크기 조정 텍스트 만들기

그래서

private float mStartTextSize; 
private TextView mWordTextView; 

로 플로트 내 텍스트 뷰를 선언 한 다음 내에서 onCreate 나는

mWordTextView = (TextView) findViewById(R.id.factTextView); 
mStartTextSize = mWordTextView.getTextSize(); 

을하지만 디버깅 내 전화를 사용하여, 그것은 105의 값을 가져옵니다 심지어 tho 나는 xml로 30sp로 설정했습니다.

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="55dp" 
    android:text="@string/choose_teams" 
    android:singleLine="false" 
    android:id="@+id/wordTextView" 
    android:textSize="30sp" 
    android:textColor="@android:color/white" 
    android:textStyle="bold|italic" 
    android:layout_below="@+id/titleTextView" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="70dp" 
    android:gravity="top|center"/> 
+0

감사합니다. 감사합니다. –

답변

0

크기는 픽셀이 아닙니다.

getTextSize 
float getTextSize() 

Returns 
float the size (in pixels) of the default text size in this TextView. 

getTextSize() documentation

당신은 상황의 필요없이 다시 픽셀에 비밀 SP/DP이를 사용할 수 있습니다.

public static float pxToDp(int px) { 
    return px/Resources.getSystem().getDisplayMetrics().density; 
} 

public static float dpToPx(int dp) { 
    return dp * Resources.getSystem().getDisplayMetrics().density; 
}