본문 바로가기
개발(Development)/Android(안드로이드)

[안드로이드] TextView 임시 텍스트 설정 방법(tools:text): 개발시에만 노출, 런타임 비노출

by 카레유 2021. 9. 1.

안드로이드 개발 시,

런타임에는 표시되면 안 되는 값들이 있을 수 있다.

 

예를 들어 서버에서 데이터를 받아와 TextView에 표시하는 경우,

통신 딜레이로 인해 기존에 가라(?)로 입력해둔 값이 노출되는 문제가 발생한다.

 

하지만 개발 작업의 편의와 대략적인 레이아웃 파악을 위해서는

가라 데이터를 입력해두는게 필요할 때가 있다.

 

이러한 니즈를 충족시키기 위해

안드로이드는 tools:text 네임스페이스:속성을 통해 아래의 기능을 지원한다.

 

1. 안드로이드 스튜디오의 미리보기(Preview: Design 모드)에는 노출된다.

2. 단, 실제 앱 실행(런타임)시에는 노출되지 않는다.


# tools:text  사용방법

1. tools 네임스페이스 추가: xmlns:tools="http://schemas.android.com/tools"

2. tools:text 설정: 런타임에서 노출시키지 않을 텍스트를 android:text 속성 대신 tools:text 속성으로 교체한다.

<?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"
    tools:context=".ToolTextActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="임시데이터값"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

이렇게 설정하면,

 

- 안드로이드 스튜디오의 레이아웃 작업창(Preview: Design모드)에서는 잘 노출되지만,

 

- 실제 앱을 실행시키면 노출되지 않는다.

 


tools 네임스페이스는 이 외에도 다양한 속성을 제공한다.

이에 대한 상세한 내용은 아래 레퍼런스 참고

 

https://developer.android.com/studio/write/tool-attributes#tools_instead_of_android

 

 

 

 

댓글