In the Android layout file & #39 ;? & Amp; #39; function, androidlayout

Source: Internet
Author: User

In the Android layout file '? ', Androidlayout

In the Android layout file, attribute reference resources generally use @, for example

android:textColor="@color/white"

But in some system files, we can also see that there is such a writing method.

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

We know that @ references the defined resources, such as @ color/white, @ android: color/white, then '? ? The following is an explanation in the document.

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 essential 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. For instance:

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

For example, here's how you can reference an attribute to set the text color to match the "primary" text color of the system 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 reference the Resource Value in the topic to display the style type of the current topic.

There are several ways to set a topic,

  • In AndroidManifest, In the application tag, the topic will be applied to all the activities under the 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 tag, this only sets the topic for the activity.
<activity    android: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>
  • Set the code in the Activity, which must be called before the setContent () method
@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setTheme(R.style.AppTheme);    setContentView(R.layout.activity_main);}

Example:
Topicstyle/AppTheme

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

Topicstyle/AppTheme.Another

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

You can see that the topic sets the background of all views#ffff8800. The reason is:android:backgroundIs a common attribute of the View. If the View itself does not set this value, the value under the current topic will be used.
What if we only want to change the background color of a view? You can use?This symbol. Firstattrs.xmlDefine a tag

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

Set the tag value in the topic.

<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"                ...                >    <TextView        android: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 result is as follows:

If we define other topics, such:

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

You only need to set the corresponding topic to change the attribute value of the view.
You can also set more tags in the topic, so that you can complete a series of attributes such as the background, font color, and so on at the same time.

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.