Issue
what is the meaning of @ and ? in the layout.xml file I am new to android.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/holo_red_dark">
<ImageView
android:src="@drawable/duck"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Link"
android:textSize="?android:textAppearanceLarge"
/>
</LinearLayout>
android:textSize="?android:textAppearanceLarge"
android:background="@android:color/holo_red_dark"
Solution
- "?": it extracts a specific field/attribute/value from current "context"/object
- "@": it refers to a specific entity that exists as a standalone "thing"
"?" is used when you want to refer to a specific Attribute of a (i.e) style but this style could be changed at "any time" (think about Dark or Light theme: the "Accent" color could be light or dark but the name is always ?attr/accentColor" for both themes). So if you refers to "?android:textAppearanceLarge" you want to use the "text appearance Large" version of current applied Theme. But this "text appearance Large" version could be different between two different Themes.
"@" is used when you want to use EXACTLY a specific "thing" that can't change between themes. In your specific example "@android:color/holo_red_dark" refers to a specific color that remain the same at any Theme and it is always "statically resolvable"
Answered By - emandt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.