为资源Resource创建别名 引用style资源

来源:互联网 发布:软件用户手册模板 编辑:程序博客网 时间:2024/05/22 08:26

Crete resources aliases

you can even use resources in XML to create aliases. For example, you can create adrawable resource that is an alias for another drawable resource:

<?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android"    android:src="@drawable/other_drawable" />

This sounds redundant, but can be very useful when using alternative resource. Read more aboutCreating alias resources.


Referencing style attributes

A style attribute resource allows you to reference the valueof an attribute in the currently-applied theme. Referencing a style attribute allows you tocustomize the look of UI elements by styling them to match standard variations supplied by thecurrent theme, instead of supplying a hard-coded value. Referencing a style attributeessentially 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 resourceformat, but instead of the at-symbol (@), use a question-mark (?), and theresource 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" />

Here, the android:textColor attribute specifies the name of a style attributein the current theme. Android now uses the value applied to theandroid:textColorSecondarystyle attribute as the value forandroid:textColor in this widget. Because the systemresource 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 theattr type.


0 0
原创粉丝点击