NOTE:Android Styles and Themes的一些需要注意的小细节

本文介绍了Android中样式的继承方式及应用方法,并解释了部分属性为何仅适用于特定视图。同时,文中还提供了如何在XML文件中定义颜色资源并应用于样式属性的例子。

<p></p> <p>1. 继承: 在运用系统自带的风格时只想改变其中一些小的方面,当然,我们可以直接在布局文件中控件的属性里重写这个属性即可。因为在布局文件中定义的属性的优先级>我们在style中定义的属性。</p> <pre>&lt;style name=&quot;GreenText&quot; parent=&quot;@android:style/TextAppearance&quot;&gt;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;item name=&quot;android:textColor&quot;&gt;#00FF00&lt;/item&gt;<br />&lt;/style&gt;</pre>

若是继承自己定义的style时可以使用下面格式,继承于CodeFont.但不适用于继承系统自带的style

<pre>&lt;style name=&quot;CodeFont.Red&quot;&gt;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;item name=&quot;android:textColor&quot;&gt;#FF0000&lt;/item&gt;<br />&lt;/style&gt;</pre>

<p>2.&#160; style内容:可以上Android developer查找特定类的XML attributes,还有些属性是不属于任何view,只属于window的.</p>

<blockquote> <p>However, if you apply a style to a View that does not support all of the style properties, the View will apply only those properties that are supported and simply ignore the others.</p> </blockquote>

<p>你所定义的style,并不一定适用于所有的视图,有些属性试图并不支持,这个时候,视图将只会运用它所支持的属性,忽略掉剩下的属性.</p>

<p>3. 应用style:</p>

<pre>&lt;color name=&quot;custom_theme_color&quot;&gt;#b0b0ff&lt;/color&gt;<br />&lt;style name=&quot;CustomTheme&quot; parent=&quot;android:Theme.Light&quot;&gt;<br />&#160;&#160;&#160; &lt;item name=&quot;android:windowBackground&quot;&gt;@color/custom_theme_color&lt;/item&gt;<br />&#160;&#160;&#160; &lt;item name=&quot;android:colorBackground&quot;&gt;@color/custom_theme_color&lt;/item&gt;<br />&lt;/style&gt;</pre>

<blockquote> <p>(Note that the color needs to supplied as a separate resource here because the <code>android:windowBackground</code>attribute only supports a reference to another resource; unlike <code>android:colorBackground</code>, it can not be given a color literal.)</p></blockquote> <p>注意到<code>android:windowBackground</code>只支持对另一资源的引用,这与<code>android:colorBackground</code>不一样.</p>

转载于:https://my.oschina.net/speedinghzl/blog/170248

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值