In the development of Android project, we often use the "@" or "?" symbol to refer to the system or the resources we add in the application, what is the difference between the reference of the two symbols, "? attr/" and "android:attr/" What is the difference? In this article, we might as well chat.
References to "@" and "?" symbols have a canonical format when used:"@[+][package:]type:name","?" [ Package:][type:]name ". As you can see, both contain a reference symbol, the package to which the resource belongs, the resource type, and the resource name.
@ Resource Reference
The "@" symbol is used to refer to the system and some of the intrinsic resources (drawable,string, etc.) that we add in the project, or the style style that is defined. Like what:
1 |
android:text= "@string/app_name" |
The app_name here is our own string resource defined in the project file values/strings.xml
.
1 |
android:text= "@android: String/cancel" |
Here, cancel belongs to the system string resource in the Android SDK, so you need to add @android: to indicate the source of the reference. Android: is a specific instance of package : .
? Property Reference
The "?" symbol is used to refer to some of the property values defined in the current topic. Note that the "?" symbol indirectly refers to the corresponding attribute value in the current topic through the property name, not the property itself. As an example:
1 |
Android:divider= "? Android:listdivider" |
The "?" Symbol here indirectly obtains the value assigned to the property by the current topic through the property name Android:listdivider. As @android: General,? Android: indicates that the value originated from the Android SDK system properties. Because the value of the corresponding property name is found in the current topic, no attribute type is specified, which is actually equivalent to:android:attr/listdivider.
How do you refer to custom properties in your project? We attrs.xml
define an attribute in the, such as:
1 2 3 |
<declare-styleablename="Customtextview"> <attrname="Colortextcustom"format="Reference|color"/ > </declare-styleable> |
Obviously, the Colortextcustom attribute we define at this point has no value, and the direct reference has no effect. You need to assign a value in the theme style:
1 2 3 4 5 6 7 |
<style name= "basetheme" parent=" Theme.AppCompat.Light.NoActionBar "> <item name="Colortextcustom">#FF0000</Item> </style>
<style name="Apptheme" parent="Basetheme"> <item name="Android:textcolor">? colortextcustom</Item > </style> |
As you can see, the Colortextcustom property is assigned in Basetheme, and the property value is referenced in Apptheme through "? Colortextcustom". The android: namespace is not added because it is a property defined in the local project. In fact, the benefit of this approach is that Apptheme's View can be used by constructors to get the value of the Colortextcustom property in the current topic.
R.attr & R.style
There are many properties and themes that are defined in the Android SDK for use, as described in the official documentation: R.ATTR & R.style. The advantage of using system resources is that it is more flexible to meet the needs of different systems.
Here are a few common:
Style= "? Android:attr/borderlessbuttonstyle"
Android 5.0 The default Button style comes with a border shadow, which you can use to remove the style from this system style. Of course, this is a separate set-up operation, in order to facilitate global control, you can styles.xml
customize a style in, inherit a borderless style as the parent:
1 2 3 4 |
<stylename="Customborderlessbuttonstyle"parent="@style/ Widget.AppCompat.Button.Borderless "> <itemname="Android:textcolor">@android: Color/white</ Item> ... </Style> |
Android:background= "? Android:attr/selectableitembackground"
Can be used to set some background with click effects such as List Item, button. This style comes with a touch-click Effect, with the Ripple ripple effect on the 5.0 and later versions, eliminating the process of implementing the selector selector ourselves. Of course, we can also use <ripple>
the label to define a drawable file for ripple effects, just pay attention to the version limit. For example, use <ripple>
the tag to create a Floatingactionbutton-like button (Minsdkversion is 21):
Create a new Res/drawable/shape_ripple.xml file to build a FAB-like shape:
1 2 3 4 5 |
<?xml version= "1.0" encoding= "Utf-8"?> <shapexmlns:android="http://schemas.android.com/apk/res/android"android: Shape="Oval"> <CornersAndroid:radius="48DP"/> <solidandroid:color="@android: Color/white" /> </Shape> |
Create a new Res/drawable/ripple_fab.xml file to build ripple touch effects:
1 2 3 4 |
<? XML version= "1.0" encoding= "Utf-8", < Ripple android:color = Span class= "attr" >xmlns:android = <item android:drawable =; </item ;
</ripple ; |
Applied in the Button control in the layout file:
1 2 3 4 5 6 7 8 9 10 11 12 |
<Button android:layout_width="56DP" android:layout_height="56DP" android:minheight="0DP" android:minwidth="0DP" Android:text="+" android:textsize="40sp" android:gravity="center" Android:background="@drawable/ripple_fab" app:elevation="1DP" android:fontfamily="Sans-serif-light" android:layout_alignparentend="true"/> |
Based on this processing, the effect of the final run-time
Android:background= "? android:attr/dividervertical"
Implements the split line background.
There are some other useful system resources, not listed here ...
Chat about @ and in Android? The reference difference between symbols