본문 바로가기
프로그래밍/안드로이드

#9.안드로이드 뷰 속성

by -현's- 2023. 1. 15.
반응형
안드로이드 뷰 속성

 

안드로이드 뷰 속성

html로 비교하면 뷰 태그는 html태그, 뷰 속성은 html태그의 속성과 비슷하다고 보면 됩니다.

id를 지정할 수도 있고, 가로세로 길이 지정, background, textColor, padding, margin 등 다양한 속성들을 지정할 수 있습니다.

레이아웃 xml영역을 html, 액티비티 자바를 자바스크립트 영역이라고 생각하면 됩니다.

안드로이드도 결국 클라이언트 프로그램이니까요.

 

*layout_width, layout_height

뷰의 가로세로 길이를 설정합니다. 이 2가지는 반드시 있어야 하는 값입니다.

이 값이 있어야 가로세로 길이를 결정할 수 있기 때문입니다.

속성값은 match_parent, wrap_content, 00px 3가지로 지정할 수 있습니다.

match_parent는 해당뷰를 감싸고 있는 상위뷰의 크기에 맞게 설정하는 속성값입니다.

wrap_content는 해당 뷰가 감싸고 있는 내용에 맞게 설정하는 속성값입니다.

00px는 구체적인 수치로 설정하는 속성값입니다.

스마트폰 크기가 다 다르다보니 수치로 값을 지정하기보다는 match_parent나 wrap_content로 지정하는 게 좋습니다.

반응형

*id

속성에 id값을 설정할 수 있습니다.

단순히 고정된 내용을 출력하려면 id를 세팅 안해도 되지만, 동적으로 값을 바꾸려면 id값을 세팅해야 합니다.

id를 설정하면 자바액티비티 영역에서 해당 id를 이용하여 뷰 객체를 가지고 올 수 있습니다.

html태그에 id를 설정하고 자바스크립트에서 가지고 와서 사용하는 것과 비슷하다고 보면 됩니다.

실제 안드로이드도 DOM구조로 되어 있습니다.

 

*예제 1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/testId1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World1!" />

    <TextView
        android:id="@+id/testId2"
        android:layout_width="match_parent"
        android:layout_height="200px"
        android:text="Hello World2!"
        android:textColor="@color/purple_200"
        android:background="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

 

 

*예제 2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/testId1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World1!"
        android:background="@color/teal_200"/>

    <TextView
        android:id="@+id/testId2"
        android:layout_width="wrap_content"
        android:layout_height="200px"
        android:text="Hello World2!"
        android:textColor="@color/purple_200"
        android:background="@color/black"/>

    <TextView
        android:id="@+id/testId3"
        android:layout_width="wrap_content"
        android:layout_height="200px"
        android:text="Hello World3!"
        android:background="@color/teal_200"
        android:layout_margin="30dp"/>
    <TextView
        android:id="@+id/testId4"
        android:layout_width="wrap_content"
        android:layout_height="200px"
        android:text="Hello World4!"
        android:textColor="@color/purple_200"
        android:background="@color/black"
        android:paddingRight="20dp" />

</LinearLayout>

2023.01.23 - [프로그래밍/안드로이드] - #10.안드로이드 TextView, EditText, Button

 

#10.안드로이드 TextView, EditText, Button

TextView - 문자열을 출력하는 뷰이다. ●text : 문자열을 지정하는 속성이다. ●textStyle : 문자열에 효과를 주는 속성이다.normal, bold, italic 값을 지정할 수 있다. ●textColor : 문자열에 색상을 지정할

hyunssssss.tistory.com

 

반응형

댓글