Style Resource

A style resource defines the format and look for a UI. A style can be applied to an individual View (from within a layout file) or to an entire Activity or application (from within the manifest file).

For more information about creating and applying styles, please readStyles and Themes.

Note: A style is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine style resources with other simple resources in the one XML file, under one <resources> element.

FILE LOCATION:
res/values/filename.xml
The filename is arbitrary. The element's  name will be used as the resource ID.
RESOURCE REFERENCE:
In XML:  @[package:]style/style_name
SYNTAX:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style
        name="style_name"
        parent="@[package:]style/style_to_inherit">
        <item
            name="[package:]style_property_name"
            >style_value</item>
    </style>
</resources>
ELEMENTS:
<resources>
Required. This must be the root node.

No attributes.

<style>
Defines a single style. Contains  <item> elements.

attributes:

name
StringRequired. A name for the style, which is used as the resource ID to apply the style to a View, Activity, or application.
parent
Style resource. Reference to a style from which this style should inherit style properties.
<item>
Defines a single property for the style. Must be a child of a  <style> element.

attributes:

name
Attribute resourceRequired. The name of the style property to be defined, with a package prefix if necessary (for example  android:textColor).
EXAMPLE:
XML file for the style (saved in  res/values/):
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomText" parent="@style/Text">
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">#008</item>
    </style>
</resources>
XML file that applies the style to a  TextView (saved in  res/layout/):
<?xml version="1.0" encoding="utf-8"?>
<EditText
    style="@style/CustomText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:text="Hello, World!" />

样式资源定义的格式,并寻找一个UI。样式可以应用于单个视图(从布局文件内)或整个活动(从清单文件内)或应用程序。

有关创建和应用样式的更多信息,请阅读 样式和主题

注意:样式是使用在所提供的值引用的一个简单的资源名称属性(未XML文件的名称)。因此,你可以风格的资源与其他资源的简单一个XML文件中结合起来,在一个<资源>元素。

文件位置:
RES /价值/ 文件名 ​​的.xml
的文件名 ​​是任意的。元素的 名字将被用作资源ID。
资源引用:
在XML: @ [包:]风格/ STYLE_NAME
句法:
<?xml的version = "1.0" encoding = "utf-8" ?> 
< resources > 
    < style
         name = " style_name " 
        parent = "@[package:]style/ style_to_inherit " > 
        < item
             name = " [package:]style_property_name " 
            > style_value </ item > 
    </style> 
</resources>
内容:
<资源>
必选项。这必须是根节点。

无属性。

<STYLE>
定义一个样式。包含 的<item>元素。

属性:

名称
字符串必需。一种用于样式,其被用作资源ID的样式应用于一个视图,活动,或应用程序的名称。
样式资源。参考从中这种风格要继承样式属性的样式。
<项目>
定义样式的一个属性。必须是一个孩子  的<style>元素。

属性:

名称
属性资源必需。要定义的样式属性的名称,以包前缀,如果有必要(例如 机器人:文字颜色)。
例:
对于风格的XML文件(保存在 RES /价值/):
<?XML版本= “1.0” 编码= “UTF-8” ?> 
<资源> 
    <样式 名称= “CustomText”  = “@风格/文本” > 
        < 项目名称= “机器人:TEXTSIZE” > 20SP </ > 
        < 项目名称= “机器人:文字颜色” >#008 </ 项目> 
    </作风> 
</资源>
该样式应用到一个XML文件中 的TextView (保存在 RES /布局/):
<?xml的 

    
    
    
    世界!“  />

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值