4

I want to use a style of Android libraries. I think I should use @ (Case 2) for style attribute, but I saw a sample from website, it uses ? (Case 1). I don't know why.

Does Android lib include both the named listSeparatorTextViewStyle resource and the style named listSeparatorTextViewStyle attribute ? Yes, I did find the style named listSeparatorTextViewStyle attribute in system android lib attrs.xml, but where can I find the named listSeparatorTextViewStyle resource ?

Here is the code and effect

Case 1

<TextView
    android:id="@+id/dialog_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    style="?android:attr/listSeparatorTextViewStyle"  
/>

Case1.GIF

Case 2

<TextView
    android:id="@+id/dialog_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    style="@android:attr/listSeparatorTextViewStyle"  
/>

Case2.GIF

HelloCW
  • 843
  • 22
  • 125
  • 310
  • http://stackoverflow.com/questions/11158349/android-styles-difference-between-style-androidstyle-xyz-and-style-and?rq=1 – VM4 Aug 15 '14 at 07:33

2 Answers2

2

Referencing resource attributes:

  • Resources can be referenced from code using integers from R.java, such as R.drawable.myimage
  • Resources can be referenced from resources using a special XML syntax, such as @drawable/myimage

Referencing style attributes:

  • A style attribute resource allows you to reference the value of an attribute in the currently-applied theme. Referencing a style attribute allows you to customize the look of UI elements by styling them to match standard variations supplied by the current theme, instead of supplying a hard-coded value. Referencing a style attribute essentially says, "use the style that is defined by this attribute, in the current theme."

  • To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (@), use a question-mark (?), and the resource type portion is optional.

  • ?[<package_name>:][<resource_type>/]<resource_name>

EX::

   <EditText id="text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="?android:textColorSecondary"
    android:text="@string/hello_world" />
  • Here, the android:textColor attribute specifies the name of a style attribute in the current theme. Android now uses the value applied to the android:textColorSecondary style attribute as the value for android:textColor in this widget. Because the system resource tool knows that an attribute resource is expected in this context, you do not need to explicitly state the type (which would be ?android:attr/textColorSecondary)—you can exclude the attr type.

Reference:: Android Docs - Its all there

Devrath
  • 42,072
  • 54
  • 195
  • 297
0

First of interesting question!

I took a look at the reference guide for R.stylable it has the following mentioned in several parts of the text:

Must be a reference to another resource, in the form @[+][package:]type:name or to a theme attribute in the form ?[package:][type:]name.

After finding this page i got a full explanation:

Here is the syntax to reference a resource in an XML resource:

@[<package_name>:]<resource_type>/<resource_name>
  • <package_name> is the name of the package in which the resource is located (not required when referencing resources from the same package)
  • <resource_type> is the R subclass for the resource type
  • <resource_name> is either the resource filename without the extension or the android:name attribute value in the XML element (for simple values).

To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (@), use a question-mark (?), and the resource type portion is optional. For instance:

?[<package_name>:][<resource_type>/]<resource_name>

Conclusion: Use @ when referencing a resource in XML and ? when referencing a style attribute. If you didn't catch it all i highly recommend reading the Accessing Resources guide.

Rawa
  • 13,357
  • 6
  • 39
  • 55
  • Thanks! Do you mean the android lib include both the named listSeparatorTextViewStyle resource and the style named listSeparatorTextViewStyle attribute ? Yes, I did find the style named listSeparatorTextViewStyle attribute in system android lib attrs.xml, but where can I find the named listSeparatorTextViewStyle resource ? – HelloCW Aug 15 '14 at 08:31