安卓主题和风格

根据网络上的说法,安卓UI发生两次大的改动。一次是android 3.0发布,UI开发支持了Fragment,主要增加了大屏幕显示的支持,这个版本就开始支持Holo Theme,由于android 3.X的设备占有率也不高,这一次的改变没有引起大的关注;再一次的改变就是Android 4.0,也就是通常所说的ICS(Ice Cream Sandwich),这个于2011年底发布的Android系统,同时也发布了指导性的应用设计规范《Android Design》有了设计规范的指导,就有了更多应用采用了Holo Theme,尤其国外的应用。Holo Theme的主要特点是轻快的颜色、适当的阴影、卡片化布局、方角矩形。

 

主题和风格的自定义都放在res/values/styles.xml文件中。xml文件部分如下:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <--主题-->

    <style name="AppBaseTheme" parent="android:Theme.Light">

</style>

<--风格-->

    <style name="progress_wait" parent="@android :style/Widget.ProgressBar.Horizontal">

        <item name="android:indeterminateOnly">true</item>

        <item name="android:indeterminateDrawable">@anim/progress_wait</item>

    </style>

</resource>

一 主题(Theme)

A theme is a style applied to an entire Activity or application, rather than an individual View. 最简单浅显的比喻就是,你在使用Android Studio的时候,有好几个主题可以选,其中一个是Windows,一个是Intellij,调整后整个界面的主题就变了。这里的主题,类似于安卓里面的主题。

例如,在用Eclipse新建安卓工程的时候,主题选项如下图,目前安卓的风格也就如下几种:

图一 Eclipse新建工程师供选择的主题

主题效果如下(图片来自网络),从左到有的效果,分别是:Dark,Light,Light with Dark Action Bar:

图二 各种风格效果图

主题放置的位置在<application>标签或者是<activity>标签,如:

<application android:theme="@style/AppTheme"></application>

<activity android:theme="@style/AppTheme"></activity>

 

二 风格(Style)

参见官方文档。鄙人稍作翻译。

style is a collection of properties that specify the look and format for a View or window. A style can specify properties such as height, padding, font color, font size, background color, and much more. A style is defined in an XML resource that is separate from the XML that specifies the layout.

风格就是用于指定视图或窗口的风格和格式的集合。风格可以指定的属性有如下,但不限于以下,高度,内间距,字体颜色,字体大小,背景色,等等。一组风格定义在xml文件中,不同于用于布局的xml文件。

Styles in Android share a similar philosophy to cascading stylesheets in web design—they allow you to separate the design from the content.

安卓中的风格类似于CSS的共用思想:设计和内容分离。

For example, by using a style, you can take this layout XML:(你可以通过如下方式使用风格)

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#00FF00"
    android:typeface="monospace"
    android:text="@string/hello" />

And turn it into this:(也可以转变成如下)

<TextView
    style="@style/CodeFont"
    android:text="@string/hello" />

All of the attributes related to style have been removed from the layout XML and put into a style definition called CodeFont, which is then applied with the style attribute.

所有与风格相关的属性全部从布局文件中移除,定义到风格资源文件写,并冠名CodeFont,这就是我们稍后要应用到的风格属性。

转载于:https://my.oschina.net/u/2245029/blog/685869

要实现简单的主题风格,可以使用 Android studio 提供的资源文件和样式表来定义组件的外观和行为。以下是实现自定义样式的步骤: 1. 在 res/values 目录下创建一个新的 styles.xml 文件,用于定义样式表。 2. 在 styles.xml 文件中定义一个基础样式,例如: ``` <style name="MyButtonStyle" parent="Widget.AppCompat.Button"> <item name="android:textColor">#FFFFFF</item> <item name="android:background">#FF0000</item> </style> ``` 这里创建了一个名为 MyButtonStyle 的样式,继承自 AppCompat 库中的 Button 组件,并设置了文本颜色和背景颜色。 3. 在 styles.xml 文件中定义一个主题,例如: ``` <style name="MyTheme" parent="Theme.AppCompat.Light"> <item name="android:colorPrimary">#FF0000</item> <item name="android:colorPrimaryDark">#990000</item> <item name="android:textColorPrimary">#FFFFFF</item> <item name="android:textColorSecondary">#CCCCCC</item> <item name="android:windowBackground">#FFFFFF</item> <item name="android:buttonStyle">@style/MyButtonStyle</item> </style> ``` 这里创建了一个名为 MyTheme 的主题,继承自 AppCompat 库中的 Light 主题,并设置了主题色、文本颜色、窗口背景色和按钮样式等属性。 4. 在 AndroidManifest.xml 文件中指定使用该主题,例如: ``` <application android:theme="@style/MyTheme" ...> ... </application> ``` 这里将应用程序的主题设置为 MyTheme。 5. 在布局文件中使用自定义样式,例如: ``` <Button android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/MyButtonStyle" android:text="My Button" /> ``` 这里将 Button 组件的样式设置为 MyButtonStyle。 通过以上步骤,就可以实现简单的主题风格,并给组件自定义样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值