세라쌤의 IT 튜토리얼

TextView 본문

Android

TextView

issell 2019. 3. 25. 10:07

DOCS : https://developer.android.com/reference/android/widget/TextView


TextView는 다음과 같은 상속 구조를 가진다.


 java.lang.Object
   ↳android.view.View
    ↳android.widget.TextView

 


TextView는 Button, EditText, CheckBox, RadioButton, ToggleButton등의 부모 클래스이기도 하다.


TextView란?

사용자에게 Text를 보여주는 용도로 사용하는 위젯이다. 

TextView의 주요 속성 

 android:layout_width

 (필수) 위젯의 너비

 android:layout_height

 (필수) 위젯의 높이

 android:text

 사용자에게 보여줄 텍스트 

 android:textSize

 텍스트의 크기. 보통 sp로 지정할 것을 추천한다. 

 android:textColor

 텍스트의 색상.  

 anrdoid:background

 위젯의 배경. 색상과 Drawables의 png/jpg/xml 등을 지정할 수 있다. 

 android:gravity

 위젯 내의 content(TextView의 경우, 텍스트)가 위젯의 어디에 위치할 지 




xml로 설정

 <TextView

android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:textSize="40sp"
android:textColor="#908be0"
android:background="#edbcbb"
android:gravity="center_vertical|right"
/>

참고) 안드로이드 Color 지정하기



java로 설정

TextView tv = findViewById(R.id.text_view); // xml에 선언된 id 값
tv.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
tv.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
tv.setText("Hello, World!");
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40);
tv.setTextColor(Color.rgb(0x90, 0x8b, 0xe0));
tv.setBackgroundColor(Color.rgb(0xeb, 0xbc, 0xbb));
tv.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);



결과 







'Android' 카테고리의 다른 글

안드로이드 Color 지정하기  (0) 2019.03.25
android:layout_width와 android:layout_height  (0) 2019.03.25
Activity와 Activity 생명주기  (0) 2019.03.20
Widgets  (0) 2019.03.17
Frame Layout  (0) 2019.03.17
Comments