有的时候我们一个页面要用很多个textview,而且这些textview的样式非常相像,这种情况下我们可以把这些样式抽取出来,然后在每个textview中引用即可,这样修改起来也方便。

我们来看一个简单的例子,在一个页面中有三个textview,每个textview显示内容不同但是样式都相像,先来看看显示效果:

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.style.MainActivity" > <TextView
style="@style/tv_style"
android:text="纵豆蔻词工,青楼梦好,难赋深情。" />
<TextView
style="@style/tv_style_g"
android:text="二十四桥仍在,波心荡、冷月无声。" />
<TextView
style="@style/tv_style.tv_style_b"
android:text="念桥边红药,年年知为谁生。" /> </LinearLayout>

布局中的textview很简洁,因为我们把显示样式都抽取出来了,看看样式:

<resources>
<style name="AppBaseTheme" parent="android:Theme.Light" />
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme" /> <style name="tv_style">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">25sp</item>
<item name="android:textColor">#FF0000</item>
</style>
<style name="tv_style_g" parent="tv_style">
<item name="android:textColor">#00FF00</item>
</style>
<!-- 两种不同的继承方式 -->
<style name="tv_style.tv_style_b">
<item name="android:textColor">#0000FF</item>
</style>
</resources>

tv_style是一个总的样式,定义了每个textview的宽高,字体大小以及字体颜色,tv_style_g和tv_style_b是继承自tv_style样式,继承之后覆盖了tv_style中的颜色,其他都不变,这里有两种继承方式。

android中的样式文件可以用于任何控件,包括button,imageview等。

这是控件的样式,应用的主题我们也可以自己设置:

在AndroidManifest.xml文件中,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.style"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

android:theme=”@android:style/Theme.Translucent”表示应用的主题,Translucent表示是透明的,效果如图:

打开应用后就是这种效果。

还有一种是Dialogandroid:theme="@android:style/Theme.Dialog,这个表示是以对话框的形式显示,效果如图:

还有很多种,看官自己去试吧。

完整代码下载

android中的样式和主题的更多相关文章

  1. android中的样式主题和国际化

    一.Android中的样式和主题     1.1样式     样式是作用在控件上的,它是一个包含一个或者多个view控件属性的集合.android style类似网页设计中的css设计思路,可以让设计 ...

  2. [android] 安卓自定义样式和主题

    简单练习自定义样式和主题,样式是加在View上,主题是加在Application或者Activity上 styles.xml <?xml version="1.0" enco ...

  3. Android中EditText样式修改 聚焦光标、背景

    在Android开发中,根据项目的需求,需要定制一些特殊的样式,例如:使用EditText时,聚焦时的背景及光标图片使用自定义而非android系统默认的.这两天,在项目中涉及此需求,现记录如下: 首 ...

  4. Android下Notification,样式style,主题theme的功能实现

    一:Notification 1.NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVIC ...

  5. Android中Context样式分析

    目录 1.样式定义以及使用 1.1.默认样式 1.2.样式定义及使用 1.3.当前样式下attr属性的获取 1.4.属性集合的定义与获取 2.Activity中Theme的初始化流程 2.1.系统调用 ...

  6. Android中自定义样式与View的构造函数中的第三个参数defStyle的意义

    零.序 一.自定义Style 二.在XML中为属性声明属性值 1. 在layout中定义属性 2. 设置Style 3. 通过Theme指定 三.在运行时获取属性值 1. View的第三个构造函数的第 ...

  7. Android中为窗口定义主题

    在res/values/styles文件夹中定义如下: <style name="myTheme"> <item name="android:windo ...

  8. android 中theme样式的解释

    android:theme="@android:style/Theme.Dialog" : Activity显示为对话框模式 android:theme="@androi ...

  9. Android 样式和主题(style & theme)

    Android 样式 android中的样式和CSS样式作用相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个view控件属性的集合.如:需要定义字体的颜色和大小. 在CSS中是这样定义的 ...

随机推荐

  1. <c:if>判断参数是否为空

    EL表达式判断字符串是否为空: <c:if test="${empty str}"> str为空</c:if> <c:if test="${ ...

  2. svn删除目录后提交显示Item 'XXXX' is out of date解决方法

    1.在要删除的目录上执行 svn 的 Delete 2.来到要删除目录的上级目录,执行更新操作.  3.找到要删除的目录,会显示冲突状态,在这个目录上执行Resolved.  4.在这个要删除的目录上 ...

  3. 【HDOJ】4553 约会安排

    线段树.线段树的细节很重要,小数据遍历可以发现问题. /* 4553 */ #include <iostream> #include <string> #include < ...

  4. asp.net中bin目录下的 dll.refresh文件

    首先找到了这篇文章http://www.cnblogs.com/haokaibo/archive/2010/07/31/1789342.html 然后找到一篇英文的文章http://monsur.xa ...

  5. JVM Monitoring: JMX or SNMP?

    JVM Monitoring: JMX or SNMP? By daniel on Feb 23, 2007 Since JavaTM SE 5.0, the JRE provides a means ...

  6. index 辨别字符在字符串中的位置

    namespace index{    class Program    {        static void Main(string[] args)        {            wh ...

  7. scp 在不同主机之间数据传输

    不同的Linux之间copy文件常用有3种方法,第一种就是ftp,也就是其中一台Linux安装ftp Server,这样可以另外一台使用ftp的client程序来进行文件的copy.第二种方法就是采用 ...

  8. Bzoj 1598: [Usaco2008 Mar]牛跑步 dijkstra,堆,K短路,A*

    1598: [Usaco2008 Mar]牛跑步 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 427  Solved: 246[Submit][St ...

  9. Linux I2C设备驱动编写(三)-实例分析AM3359

    TI-AM3359 I2C适配器实例分析 I2C Spec简述 特性: 兼容飞利浦I2C 2.1版本规格 支持标准模式(100K bits/s)和快速模式(400K bits/s) 多路接收.发送模式 ...

  10. GRUB损坏后,如何修复windows启动mbr

    今天使用Ghost装系统遇到windows7不能启动的问题,采用下面帖子中的部分命令搞定之. 我自己是直接使用: 插入windows7安装光盘,从光盘启动,在光盘启动完成后,按下shift+f10键, ...