您的位置:首页 > Web前端 > CSS

样式与主题

2015-06-11 17:12 513 查看


原文链接:https://github.com/codepath/android_guides/wiki/Styles-and-Themes


概述

风格在Android允许你定义的外观,例如颜色和字体,在XML资源文件的Android组件。这样你只能设置一次,在一个地方集中常见的样式属性。这通常是在CSS非常类似于在Web开发世界的一种方式,减少重复使用的造型。在一个中央文件中指定的样式,我们可以应用一致的造型在我们的应用程序的视图。


使用

结合图片款式是如何更多的意见保持维持在重UI定制的脸。样式定义风格的名字与一系列性能有关的申请期工作。款式还可以从其他方式和复合方式可以创建和继承。


定义和使用方式

第一,你定义XML的样式在
res/values/styles.xml
:

<style name="LargeRedFont">
<item name="android:textColor">#C80000</item>
<item name="android:textSize">40sp</item>
</style>


现在你可以在你的activity中使用的方式:

<TextView
android:id="@+id/tv_text"
style="@style/LargeRedFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />


继承方式

在许多情况下,你需要扩展样式和修改某些属性。在<<样式元素的parentattribute允许您指定一个风格,你的风格应该继承的属性。你可以使用它从一个现有的样式中继承属性,然后只定义你想要改变或添加的属性。

<style name="LargeFont">
<item name="android:textSize">40sp</item>
</style>

<style name="LargeBlueFont" parent="@style/LargeFont">
<item name="android:textColor">#00007f</item>
</style>如果你想从中继承的样式,你定义你自己,你甚至不必使用父属性。相反,作为一种快捷方式只是前缀要继承你的新样式的名称的样式的名称,由句点分隔:<style name="LargeFont">
<item name="android:textSize">40sp</item>
</style>

<style name="LargeFont.Red">
<item name="android:textColor">#C80000</item>
</style>

你可以继续扩展样式继承他们利用多周期
<style name="LargeFont.Red.Bold">
<item name="android:textStyle">bold</item>
</style>

<style name="LargeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style>
你不能承受这样的Android内置样式。引用内置样式必须使用parent属性:
<style name="CustomButton" parent="@android:style/Widget.Button">
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="android:textColor">#FFFFFF</item>
</style>

使用主题

在某些情况下,我们想申请一个一致的主题,在所有的activity中。而不是应用样式到一个特定的个人观点,你可以申请一个风格的集合作为一个Activity或应用程序的主题。当你这样做时,Activity或应用程序内的每个视图将它支持的每个属性。

定义一个主题

<style name="LightThemeSelector" parent="android:Theme.AppCompat.Light">
...
</style>
这个主题包含的项目节点,经常参考其他款式或颜色:
<style name="LightThemeSelector" parent="android:Theme.AppCompat.Light">
<item name="android:windowBackground">@color/custom_theme_color</item>
<item name="android:colorBackground">@color/custom_theme_color</item>
</style>

定制主题

在许多情况下,你会希望自定义视图的默认出现在您的应用程序。例如,你可能想设置一个TextView或按钮的文字颜色为您的应用程序的默认。这可以通过定义样式继承的默认然后重写这些属性在做
res/values/styles.xml
:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- ...产生的东西 -->

<!-- 这是生成的应用程序的主题-->
<style name="AppTheme" parent="AppBaseTheme">
<!-- 这些是你的自定义属性-->
<item name="android:buttonStyle">@style/Widget.Button.Custom</item>
<item name="android:textViewStyle">@style/Widget.TextView.Custom</item>
</style>

<!-- 这是该应用程序的自定义按钮样式 -->
<style name="Widget.Button.Custom" parent="android:Widget.Button">
<item name="android:textColor">#0000FF</item>
</style>

<!-- 这是该应用程序的自定义文本风格 -->
<style name="Widget.TextView.Custom" parent="android:Widget.TextView">
<item name="android:textColor">#00FF00</item>
</style>
</resources>
注意,我们使用我们确定这些控件的默认样式为Button样式和TextView风格的修改产生的主题 。接下来,我们从默认的控件继承。按钮或widget.textview采取默认的方式使我们的变化。这样的结果是默认的文本颜色是不同的按钮和文本:

搞不清楚这样式属性定义一个主题时,你可以使用?这里有一些资源:themes.xml 对于一个应用程序的默认样式的成千上万的完整列表。
R.attr 一个完整的概要文件等。
Useful holo theme generator tool 或创建一个主题,有一个修改某些控件的默认颜色。

Customizing Action Bar styles guide and sample code 为主题的动作条。

应用主题

为所有的应用程序的活动设定一个主题,打开AndroidManifest.xml文件和编辑标签包括Android主题属性与样式的名称。例如:
<application android:theme="@style/CustomTheme">
你也可以申请清单中的特定Activity:
<activity android:theme="@style/CustomTheme">
你可以看到更多关于这一切在官方风格指南
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: