ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [안드로이드 kotlin] 안드로이드 스튜디오 xml 디자인에서만 보이게 하기
    안드로이드(kotlin)/UI 2021. 5. 17. 12:02

    xml 로 화면 작업을 하다보면 textview 에 글자를 숨겨서 표시해야 하는 경우 내용물을 다 지우거나 visible 을 사용합니다.

    이렇게 하면 xml 에서도 표시가 되지 않게 됩니다.

    디자인 화면상에만 표시하고 에뮬레이터나 실제 단말에서는 숨겨서 표시하고 싶은 경우도 있는데요 이럴때는 tools 를 이용하면 됩니다.

     

    혹시 xml 의 최상위 layout 에 xmlns:tools="http://schemas.android.com/tools" 가 없다면 내용을 추가해주셔야 사용 가능합니다.

     

    예시를 보여드릴게요.

    <tools 사용 전 화면과 코드>

    에뮬레이터 화면
    xml design 화면

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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"
        android:orientation="vertical"
        tools:context=".MainActivity">
        
        <TextView
            android:id="@+id/TextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:background="@drawable/circle_blue"
            android:gravity="center"
            android:text="1"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold"
            android:visibility="gone"
            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"
            app:layout_constraintVertical_bias="0.403" />
     
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="동그라미는 xml 디자인에서만 보임"
            android:textColor="@color/black"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    cs

     

     

    <tools 사용 후 화면과 코드>

    에뮬레이터 화면
    xml design 화면

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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"
        android:orientation="vertical"
        tools:context=".MainActivity">
     
        <TextView
            android:id="@+id/TextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:background="@drawable/circle_blue"
            android:gravity="center"
            android:text="1"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold"
            android:visibility="gone"
            tools:visibility="visible"
            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"
            app:layout_constraintVertical_bias="0.403" />
     
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="동그라미는 xml 디자인에서만 보임"
            android:textColor="@color/black"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    cs

     

    두 곳의 차이는 xml 코드의 23 번째 라인의 tools:visibility="visible" 입니다.

     

    tools 를 사용함으로 인해서 xml design 에만 표시되게 할 수 있습니다.

    다 숨기면 내용이 많을 경우 헷갈릴수도 있는데 이걸 이용하면 헷갈리지 않고 작업할 수 있읅것같습니다.

    댓글

Designed by Tistory.