본문 바로가기

Android

[Android] Use a default image

* Use a default image

- It's fairly common that the contents of a design might be defined dynamically at runtime—for example, any app that grabs data from the internet should probably start with a blank or empty screen.

- But it's helpful when you're designing an app to have some sort of placeholder data in the layout so you know what you're laying out.

 

 

 

1. In activity_layout.xml, copy the android:src line, and paste a second copy. Change the word "android" to "tools", so your two attributes look like this:

 

android:src="@drawable/empty_dice" 
tools:src="@drawable/empty_dice" />

 

Here you've changed the XML namespace of this attribute from the default android namespace to the tools namespace. The tools namespace is used when you want to define placeholder content that is only used in the preview or the design editor in Android Studio. Attributes using the tools namespace are removed when you compile the app.

 

Namespaces are used to help resolve ambiguity when referring to attributes that have the same name. For example, both these attributes in the <ImageView> tag have the same name (src), but the namespace is different.

 

 

2. Change the tools:src attribute in the ImageView tag to be dice_1 instead of empty_dice:

 

android:src="@drawable/empty_dice" tools:src="@drawable/dice_1" />

 

Notice that the dice_1 image is in place now as the placeholder image in the preview.

* 요약

tools 네임스페이스는 preview 또는 Android Studio의 design editor에서 자리에 표시되는 content를 보려고 할 때만 사용된다. tools 네임스페이스를 사용하는 속성은 앱을 컴파일할 때 제거된다. 

네임 스페이스는 동일한 이름을 가진 속성을 참조 할 때 모호성을 해결하는 데 사용된다. (예를 들어, <ImageView>태그에있는 두 속성은 src는 이름이 같지만 네임 스페이스는 다르다.)

 

 

참고: https://codelabs.developers.google.com/codelabs/kotlin-android-training-images-compat/#4

 

Android Kotlin Fundamentals 01.3: Image resources and compatibility

At the end of the last codelab, you had an app that updates a text view with a number between 1 and 6 each time the user taps a button. However, the app is called DiceRoller, not 1-6 Number Generator, so it would be nice if the dice actually looked like di

codelabs.developers.google.com