android reference resource @ with attribute? cheat sheet

A few days ago I stumbled across a study cheat sheet I made in the early days of Android development, put together to understand the syntax of how to handle Android resources and theme properties.

Surprisingly, I found it very useful to me now, so I decided to organize it into a more blog-friendly format and share it with everyone.

Knowing the topic we are going to discuss today, please see the following, several ways to set the background color of a view through xml layout:

android:background="@color/colorPrimary"  
android:background="@com.myapp:color/colorPrimary"  
android:background="?colorPrimary"  
android:background="?attr/colorPrimary"  
android:background="?com.myapp:attr/colorPrimary"  
android:background="?com.myapp:colorPrimary"  
android:background="?android:colorPrimary"  
android:background="?android:attr/colorPrimary"

Exciting enough, right? Well, I wish I could take it apart so it's less intimidating.

Referencing resources vs. referencing style attributes
Let's break the topic and talk about the basics of Android, because it's important to understand the difference between @ and ? before explaining further.

When we use @ - we are referencing an actual value (color, string, dimension, etc.). This resource must have a specific value, in which case we know the specific value we are dealing with.

For example

app/src/main/res/values/color.xml

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <color name="colorPrimary">#3F51B5</color>
</resources>

So when we want to reference it in xml (android:background="@color/colorPrimary"), no matter what theme the activity is, the background will be set to #3F51B5.

Conversely, when you see the ? mark - that means we are trying to reference a style attribute whose value depends on the currently used theme. I can override this property for a specific theme, so no need to change the xml layout, just apply the appropriate theme:

<resources>  
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">#F00</item>
    </style>
</resources>
<TextView  
    android:id="@+id/my_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?colorPrimary"/>

In this case, we ask Android: "Hey, give me the value of the colorPrimary property defined under the current theme". So it's hard to tell you what color the background will be, because it depends on the theme of the activity app the layout belongs to.

Syntax
Now , let's see what the syntax for referencing a resource looks like.

Reference resources (@)

@[package name:] resource type/resource name

package name - optional, that is, the name of the package to which this resource belongs (the default is the package name of your app); the reserved package - android, is used with Resources published with the system.

resource type - a subset of R, i.e. the type of resource (attr, color, string, dimen, etc.)

resource name - the name of the resource we want to reference

Let 's start with 2 examples:

android:background="@color/colorPrimary"  
android:background="@com.myapp:color/colorPrimary"

These two refer to the same resource, because the default package name is the package name of your own app, so you can leave it out:

package(optional) = com.myapp

resource type = color

resource name = colorPrimary

You may think, Android Aren't some system-level resources predefined? Yes, some built-in colors can be referenced like this:

android:background="@android:color/holo_orange_dark"

This example breaks down like this:

package = android - references built-in resources

resource type = color

resource name = holo_orange_dark

Please note:

Now, many developers use AppCompat (if you don't already, I recommend you do), and AppCompat Usually defines its own resources. Although AppCompat belongs to the lib released by Google itself, it is not part of the system. In fact, those resources are integrated into your app, so they don't need to be referenced using the android keyword.

example:

android:background="?selectableItemBackground"

Here, even though we don't have the custom attribute name selectableItemBackground in our app (note that we don't use the android: prefix here), we can still reference it because we "added" it to our app via AppCompat.

Quoting style attributes (?)

and guess what, its syntax is pretty similar to referencing resources:

?[packagename:][resourcetype/]resourcename

with a small difference:

the only resource type allowed when referencing style attributes is attr. So actually the Android packaging tools allow us to omit the resource type, so it's actually an option.

So from an Android perspective, the following expressions are actually exactly the same:

android:background="?com.myapp:attr/colorPrimary" //verbose format  
android:background="?com.myapp:colorPrimary" //attr is skipped since its optional  
android:background="?attr/colorPrimary" //package is skipped since its optional  
android:background="?colorPrimary"  // package & attr is skipped

As you can see, the syntax is super simple, no more confusion!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326306381&siteId=291194637