Android

Android Studio - UI의 TextView / Button의 속성

Cong_S 2022. 7. 8. 11:48

텍스트뷰와 버튼의 속성에 대해 알아보자.

특히 두 오브젝트는 속성이 유사하므로 같이 알아보자.

 

모든 속성은 오른쪽의 속성창을 이용하거나 코드를 수정하여 변경할 수 있다.

속성창을 이용할 땐 상단의 검색을 이용하면 빠르게 찾을 수 있다.

 


- text / textColor : text 내용을 변경하고, 색상을 변경할 수 있다.


- background : 배경색상을 변경할 수 있다.

텍스트뷰는 background 

버튼은 backgroundTint 를 변경하면 된다.


- layout_margin : 화면에서 마진(여백)을 얼마나 남길 것인지 정할 수 있다.


- visibility : 오브젝트를 보이거나 안 보이게, 또는 사라지게 만들 수 있다.

 


- padding : 마진이 화면의 여백이었다면 padding 은 문자와 문자를 감싸고 있는 프레임 간의 여백이다.

 


- gravity : 텍스트 등의 오브젝트를 정렬시키는 기능이다.

 

위 기능들을 코드로 작성하면 이런 모습이다.

 <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:background="#C6DDC7"
        android:gravity="center_vertical|center"
        android:padding="30sp"
        android:paddingLeft="30sp"
        android:paddingTop="30sp"
        android:paddingBottom="20sp"
        android:text="안녕하세요"
        android:textColor="@color/black"
        android:textSize="34sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:backgroundTint="#639F65"
        android:text="눌러봐바"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />