The role of '? ' in the Android layout file

Source: Internet
Author: User

In an android layout file, attribute reference resources typically use @, for example

android:textColor="@color/white"

But we can see that in some system files

android:textColor="?android:color/textColor"

We know that @ is referencing resources that have already been defined, such as @color/white, @android: color/white, what about '? '? Here are the explanations in the documentation

Referencing style attributes

A style attribute resource allows you to reference the value of A 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's defined by this attribute, and the current theme."

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

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

For example, here's how can reference a attribute to set the text color to match the "primary" text color of the Syst EM theme:

<EditText id="text"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:textColor="?android:textColorSecondary"    android:text="@string/hello_world" />

That is, '? ' is used to refer to the resource values in the theme to show the style type of the current theme.

The General settings topic has several methods,

    • In Androidmanifest, application tag, this will apply the theme to all the activity under application
<application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        ...</application>
    • In Androidmanifest.xml, in the Activity tab, this only sets the theme for the activity
<activityandroid:name=". Mainactivity "android:theme=" @style/apptheme "android:label=" @string/app_ Name " >                <intent-filter>        <action android:name="Android.intent.action.MAIN" />        <category android:name="Android.intent.category.LAUNCHER" />    </intent-filter></activity>
    • Code settings in the activity need to be called before the SetContent () method
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setTheme(R.style.AppTheme);    setContentView(R.layout.activity_main);}

Example:
Themestyle/AppTheme

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">    <!-- Customize your theme here. --></style>

Themestyle/AppTheme.Another

<style name="AppTheme.Another" parent="Theme.AppCompat.Light.DarkActionBar">    <item name="android:background">#ffff8800</item></style>

You can see that the theme sets all view background to #ffff8800 . The reason is android:background a general property of the view, and if the view itself does not set the value, it will use the value under the current theme.
What if we just want to change the background color of a particular view? You can use ? this symbol here. First attrs.xml define a label in the

<?xml version="1.0" encoding="utf-8"?><resources>    <attr name="customBackground" format="color|reference"></attr></resources>

Set the value of this tag in the theme

<style name="AppTheme.Another" parent="Theme.AppCompat.Light.DarkActionBar">    <item name="customBackground">#ffff8800</item></style>

Reference this topic value in the layout

<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"                ... >                    <TextViewandroid:text="@string/hello_world"android:layout_width= "Wrap_content" android:layout_height="Wrap_content"android:layout_centerinparent="true" android:background="? Attr/custombackground"android:textsize="30SP" />                                                </relativelayout>

The results are as follows

If we define some other topics, such as:

<style name="AppTheme.Third" parent="Theme.AppCompat.Light.DarkActionBar">    <item name="customBackground">#ff0099cc</item></style>

Then you only need to set the corresponding theme to change the view's property values.
You can also set more tabs in the theme so that you can change a series of attributes, such as the background, font color, and so on, like a night theme.

The role of '? ' in the Android layout file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.