상세 컨텐츠

본문 제목

[android] TextView 글자 크기 dp설정을 java코드로 설정하기 관련

스마트기기개발관련/안드로이드 개발

by AlrepondTech 2018. 3. 14. 11:59

본문

반응형
728x170

 

=================================

=================================

=================================

 

 

이런식으로 형식을 지정해주면 됩니다.

TypedValue.COMPLEX_UNIT_DIP

TypedValue.COMPLEX_UNIT_SP

 

 

예)

_obj.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);

_obj.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);

 

 

 

=================================

=================================

=================================

 

 

 

 

반응형

 

728x90

 

 

 

 

출처: https://www.androidpub.com/1323917

 

 

XML을 통해

<TextView  
android:layout_width="wrap_content" 
android:layout_height="wrap_content"  
android:textSize="20dp"
/>
 
이런식으로 설정했는데 TextView를 java에서 동적으로 생성할일이있었습니다.
TextView title = new TextView(this);
title.setTextSize(30.0f);
 
이런식으로 float으로 설정하게 되어있던데 dp값으로 셋팅은 어떻게 하나요?
java에서 생성시 styles.xml에 정의해놓은 style적용은 안되는가요?
 
--------------------------------------------------------------------------------------------------------------------------
 
float scale = getResources().getDisplayMetrics().density;
title.setTextSize(30.0f * scale);
해줘도 될 거 같은데요?
 
댓글
2011.02.25 16:58:11
id: 그냥가자그냥가자
 
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30)

이렇게 DP단위를 바로 쓸수도 있습니다.

 

 

=================================

=================================

=================================

 

 

 

출처: https://stackoverflow.com/questions/3061930/how-to-set-unit-for-paint-settextsize

 

 

Is it possible to change the unit for Paint.setTextSize()? As far as I know, it's pixel but I like to set the text size in DIP for multiple screen support.

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

I know this topic is old and already answered but I would like to also suggest this piece of code:

int MY_DIP_VALUE = 5; //5dp  int pixel= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,                               MY_DIP_VALUE, getResources().getDisplayMetrics());

 

 

=================================

=================================

=================================

 

 

반응형
그리드형


관련글 더보기

댓글 영역