Android中Style和Theme的使用

android namespace 、样式、主题 (二) NULL 博文链接:https://venus585625.iteye.com/blog/860119 立即下载

越来越多互联网企业都在Android平台上部署其客户端,为了提升用户体验,这些客户端都做得布局合理而且美观.......Android的Style设计就是提升用户体验的关键之一。Android上的Style分为了两个方面:

  1. Theme是针对窗体级别的,改变窗体样式;
  2. Style是针对窗体元素级别的,改变指定控件或者Layout的样式。

Android系统的themes.xml和style.xml(位于/base/core/res/res/values/)包含了很多系统定义好的style,建议在里面挑个合适的,然后再继承修改。

Style是View中一些属性的集合,包括height,padding,font color,background等等,Style单独定义在xml文件中,类似与web页面中css的角色,将设计和内容分开,便于修改和重复使用。

定义Style:

style文件需要保存在res/values目录下,文件名任意,但是必须是xml文件,sytle文件的根标记必须是<resources>。写了一个简单示例,效果如下:

main.xml文件代码:

[c-sharp] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent"> 
  4.     <TextView  
  5.         style="@style/CodeFont" mce_style="@style/CodeFont"  
  6.         android:text="测试style" /> 
  7. </LinearLayout> 

声明style是CodeFont,对应的是style文件中的style name。mystyle.xml文件中定义了style name是CodeFont:

[c-sharp] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <mce:style name="CodeFont" parent="@android:style/TextAppearance.Medium"><!-- 
  4.   
  5.         <item name="android:layout_width">fill_parent</item>  
  6.         <item name="android:layout_height">wrap_content</item>  
  7.         <item name="android:textColor">#00FF00</item>  
  8.         <item name="android:typeface">monospace</item>  
  9.      
  10. --></mce:style><style name="CodeFont" parent="@android:style/TextAppearance.Medium" mce_bogus="1">  
  11.         <item name="android:layout_width">fill_parent</item>  
  12.         <item name="android:layout_height">wrap_content</item>  
  13.         <item name="android:textColor">#00FF00</item>  
  14.         <item name="android:typeface">monospace</item>  
  15.     </style>  
  16. </resources> 

parent属性表示style之间可以继承,同时可以覆盖parent style的一些属性。

style继承有两种方式:

  • style的继承可以通过parent属性,用来继承android已经定义好的style,例如:
    [c-sharp] view plain copy print ?
    1. <mce:style name="GreenText" parent="@android:style/TextAppearance"><!-- 
    2.   
    3.      <item name="android:textColor">#00FF00</item>  
    4. --></mce:style><style name="GreenText" parent="@android:style/TextAppearance" mce_bogus="1">  
    5.      <item name="android:textColor">#00FF00</item> </style> 
  • 继承了android中的TextAppearance,同时覆盖了android:textColor属性。
  • 如果要继承自定义的style,不需要通过parent属性,只要style的name以需要继承的style的name开始后跟新的style的name,中间用“.”隔开。注意:这种方式只适用与自定义的style继承。

[c-sharp] view plain copy print ?
  1. <mce:style name="CodeFont.Red"><!-- 
  2.   
  3.     <item name="android:textColor">#FF0000</item>  
  4. --></mce:style><style name="CodeFont.Red" mce_bogus="1">  
  5.     <item name="android:textColor">#FF0000</item> </style> 

新的style继承了CodeFont,则在修改上边例子中的main.xml为:

[c-sharp] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent"> 
  4.     <TextView  
  5.         style="@style/CodeFont.Red" mce_style="@style/CodeFont.Red"  
  6.         android:text="测试style" /> 
  7. </LinearLayout> 


效果如下,字体颜色变为了红色:

style可以多级继承:

[c-sharp] view plain copy print ?
  1. <mce:style name="CodeFont.Red.Big"><!-- 
  2.   
  3.     <item name="android:textSize">30sp</item>  
  4. --></mce:style><style name="CodeFont.Red.Big" mce_bogus="1">  
  5.     <item name="android:textSize">30sp</item> </style> 

字号变大,效果如下:


sytle的更多属性见android包下的R.attr。需要注意,并不是所有的View都支持定义的style的属性,如果自定义的sytle中包含View不支持的属性,程序会自动忽略它。

Theme:

如果声明一个style作为Theme,需要配置mainfest文件中<activity> 或 <application>的android:theme 属性。

将自定义的style作为application的theme:

修改mystyle.xml为:

[c-sharp] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <mce:style name="CodeFont" ><!-- 
  4.   
  5.          <item name="android:textSize">20sp</item>  
  6.         <item name="android:typeface">monospace</item>  
  7.      
  8. --></mce:style><style name="CodeFont"  mce_bogus="1">  
  9.          <item name="android:textSize">20sp</item>  
  10.         <item name="android:typeface">monospace</item>  
  11.     </style>  
  12. </resources> 

在mainfest 的application中添加 android:theme属性:

[c-sharp] view plain copy print ?
  1. <application   android:icon="@drawable/icon" android:label="@string/app_name"  
  2.      android:theme="@style/CodeFont"> 

则application中的所有text字体都会改变,效果如下:


在每个<activity>标签中使用android:theme属性:

[c-sharp] view plain copy print ?
  1. <activity android:name=".MainActivity"  
  2.                  android:label="@string/app_name"  android:theme="@style/CodeFont"> 

android:theme还可以配置android中已经存在的theme:

[c-sharp] view plain copy print ?
  1. <activity android:theme="@android:style/Theme.Translucent"> 

如果想调整android已经定义好的theme,则可以通过自定义style来实现,例如:

[c-sharp] view plain copy print ?
  1. <color name="custom_theme_color">#b0b0ff</color>  
  2. <mce:style name="CustomTheme" parent="android:Theme.Light"><!-- 
  3.   
  4.     <item name="android:windowBackground">@color/custom_theme_color</item>  
  5.     <item name="android:colorBackground">@color/custom_theme_color</item>  
  6. --></mce:style><style name="CustomTheme" parent="android:Theme.Light" mce_bogus="1">  
  7.     <item name="android:windowBackground">@color/custom_theme_color</item>  
  8.     <item name="android:colorBackground">@color/custom_theme_color</item> </style> 

效果如下:


level为11以下的theme:

1、Theme:

它的意思为默认状态,即如果theme这里不填任何属性的时候,默认为Theme。

api原文为:

The default system theme. This is the theme used for activities that have not explicitly set their own theme.

You can count on this being a dark background with light text on top, but should try to make no other assumptions about its appearance. In particular, the text inside of widgets using this theme may be completely different, with the widget container being a light color and the text on top of it a dark color.

效果图如下:

1.1、Theme_NoDisplay

它的意思为任何都不显示。比较适用于只是运行了activity,但未显示任何东西。

api原文如下:

Default theme for activities that don’t actually display a UI; that is, they finish themselves before being resumed.

效果图如下:

1.2、Theme_NoTitleBar

意思为:背景主题的没有标题栏的样式,默认如果没有设置的话,显示黑背景

api原文:

Variant of the default (dark) theme with no title bar

效果图如下:

1.3、Theme_NoTitleBar_Fullscreen

意思为:背景主题的没有标题栏且全屏的样式,默认为黑背景

api原文:

Variant of the default (dark) theme that has no title bar and fills the entire screen

效果图如下:

2、Theme_Black:

它的意思为默认状态下黑背景。

api原文如下:

Special variation on the default theme that ensures the background is completely black. This is useful for things like image viewers and media players. If you want the normal (dark background) theme do not use this, use Theme.

效果图如下:

2.1、Theme_Black_NoTitleBar:

意思为:黑背景主题的没有标题栏的样式

api原文:

Variant of the black theme with no title bar

效果图如下:

2.2、Theme_Black_NoTitleBar_Fullscreen

意思为:黑背景主题的没有标题栏且全屏的样式

api原文:

Variant of the black theme that has no title bar and fills the entire screen

效果图如下:

3、Theme_Light

意思为:默认状态下亮背景,与上述黑背景Theme_Black相反。

api原文:

Theme for a light background with dark text on top. Set your activity to this theme if you would like such an appearance. As with the default theme, you should try to assume little more than that the background will be a light color.

效果图如下:

3.1、Theme_Light_NoTitleBar

意思为:亮背景主题的没有标题栏的样式,与Theme_Black_NoTitleBar相反

api原文:

Variant of the light theme with no title bar

效果图如下:

3.2、Theme_Light_NoTitleBar_Fullscreen

意思为:亮背景主题的没有标题栏且全屏显示的样式,与Theme_Black_NoTitleBa_Fullscreenr相反

api原文:

Variant of the light theme that has no title bar and fills the entire screen

效果图如下:

4、Theme_Dialog

意思为:对话框样式 将整个activity变成对话框样式出现。

api原文:

Default theme for dialog windows and activities, which is used by the Dialog class. This changes the window to be floating (not fill the entire screen), and puts a frame around its contents. You can set this theme on an activity if you would like to make an activity that looks like a Dialog.

效果图如下:这里需要自定义大小,否则显示不全。

5、Theme_InputMethod

6、Theme_Panel

意思为:删除掉所有多余的窗口装饰,在一个空的矩形框中填充内容,作用范围相当于把dialog中的所有元素全部去掉,只是一个空的矩形框,且此为默认的样式。

api原文:

Default dark theme for panel windows. This removes all extraneous window decorations, so you basically have an empty rectangle in which to place your content. It makes the window floating, with a transparent background, and turns off dimming behind the window.

效果图如下:这里需要自定义大小,否则显示不全。

6.1、Theme_Light_Panel

意思为:删除掉所有多余的窗口装饰,在一个空的矩形框中填充内容,作用范围相当于把dialog中的所有元素全部去掉,只是一个空的矩形框,且默认是light的样式。

api原文:

Default light theme for panel windows. This removes all extraneous window decorations, so you basically have an empty rectangle in which to place your content. It makes the window floating, with a transparent background, and turns off dimming behind the window.

效果图如下:这里需要自定义大小,否则显示不全。

7、

Theme_Wallpaper

意思为:使用墙纸做主题,默认状态。

api原文:

Default theme for windows that want to have the user’s selected wallpaper appear behind them.

效果图如下:

7.1、Theme_WallpaperSettings

意思为:使用墙纸做主题,默认是使用将上一个界面调暗之后作为主题,(这里我很疑惑为什么是上一个界面变暗而不是墙纸主题变暗呢?)

api原文:

Theme for a wallpaper’s setting activity that is designed to be on top of a dark background.

效果图如下:

7.2、Theme_Light_WallpaperSettings

意思为:使用墙纸做主题,默认Light状态。

api原文:

Theme for a wallpaper’s setting activity that is designed to be on top of a light background.

效果图如下:

7.3、Theme_Wallpaper_NoTitleBar

意思为:使用墙纸做主题,且没有标题栏

api原文:

Variant of the translucent theme with no title bar

效果图如下:

7.4、Theme_Wallpaper_NoTitleBar_Fullscreen

意思为:使用墙纸做主题,且没有标题栏,且全屏显示

api原文:

Variant of the translucent theme that has no title bar and fills the entire screen

效果图如下:

8、Theme_Translucent

意思为:半透明状态下的背景,将运行此activity之前的屏幕作为半透明状态作为此activity运行时的样式。

api原文:

Default theme for translucent activities, that is windows that allow you to see through them to the windows behind. This sets up the translucent flag and appropriate animations for your windows.

效果图如下:

8.1、Theme_Translucent_NoTitleBar

意思为:半透明状态下没有标题栏的背景,将运行此activity之前的屏幕作为半透明状态作为此activity运行时的样式。

api原文:

Variant of the translucent theme with no title bar

效果图如下:

8.2、Theme_Translucent_NoTitleBar_Fullscreen

意思为:半透明状态下没有标题栏且全屏的背景,将运行此activity之前的屏幕作为半透明状态作为此activity运行时的样式。

api原文:

Variant of the translucent theme that has no title bar and fills the entire screen

效果图如下:

原帖为:http://henzil.easymorse.com/?p=364

对工时评估的思考:当AI颠覆了coding效率 在AI时代,“人日”作为软件工时度量衡已然失效。开发的价值核心正从“编码实现”转向“深度思考”与“精准沟通”——即如何将人类的模糊需求精确地传达给AI。当最有价值的“思想工作”变得最难量化时,传统的项目管理范式随之动摇。我们必须超越对过程的算计,开始探索衡量思想、沟通与业务价值的新标尺。 阅读详情

相关推荐

你的App合规吗?手把手教你用Camille生成隐私调用报告给老板看

本文详细介绍了如何使用开源工具Camille进行Android App隐私合规检测,并生成高管易懂的隐私调用报告。通过动态插桩技术和多维度分析,Camille能有效识别高风险API调用,并将其映射到具体法律条款,帮助技术团队与法务、产品等非技术部门高效沟通,确保App符合《个保法》等法规要求。

weixin_33726318的博客 422

Android 入门

android与web开发很像,xml相当于html,java相当于js脚本。 控件属性 阴影 shadowColor:设置阴影颜色 shadowRadius:模糊度 以上两者必须同时使用 shadowDx:阴影水平偏移量 shadowDy:阴影竖直偏移量 singleLine true为单行显示文本,如超出界限会显示…android:singleLine="true" 事件处理 按钮事件 //点击事件 btn.setOnClickListener(new View.OnC

weixin_46823133的博客 812

麒麟桌面操作系统QT开发环境搭建

麒麟操作系统安装Qt

因为热爱,所以坚持,记录工作中的乐趣 9275

Android 开发之 themes.xml 文件

折腾了一整天在发现标题栏的颜色不能改写,全网的方案都不影响他,直到发现连activity里面的button的颜色都不能自己设置,换了一个selector后发现除了颜色之外,样式可以改动,于是最后才找出来是因为themes.xml这个文件属于系统主题色,然后里面的配置一旦被应用到 application 或者 activity 中之后,就是会最高优先级强行影响所有的元素,因此解决方案目前只能是自己写一个 style.xml 然后把他应用到 manifest 里面。

weixin_44215545的博客 919

OpenStack 8 节点生产级高可用(HA)集群实施文档

本文详细介绍了生产级高可用OpenStack集群的架构设计与实施方案。通过采用3节点控制集群(HAProxy+Keepalived)、2节点计算资源池和3节点Ceph分布式存储,实现了控制平面、计算资源和存储系统三个层面的高可用。方案重点阐述了VIP漂移、MariaDB Galera集群、RabbitMQ镜像队列等关键组件的配置方法,并提供了完整的部署流程和故障验证方案。该架构确保任意单节点故障不会影响平台整体可用性,满足生产环境对稳定性、扩展性和可维护性的要求,为构建无单点故障的OpenStack生产环境

彭大帅的博客 781

十年芯片资深IC设计工程师面经系列(四)SDC约束

set_driving_cell:当我们的设计是模块级(Block level)的design时,我们的设计前面一般还有其他的设计,那么前面模块的输出便是我们模块的输入,5.1set_false_path:可以单独的时钟或者路径进行约束。set drive :当我们的设计是芯片级(Chip level)的顶层设计时,芯片外围电路要驱动的是负载较大的IO单元,故需要的驱动能力较强,:set_load 是指output端口的负载,设置的值会影响输出到端口的net上的电容,从而影响相应的路径延时。

weixin_47349046的博客 1773

android 主题xml,自定义Android主题风格theme.xml方法 Android开发技术

自定义Android主题风格theme.xml方法 Android开发技术2013 年 5 月 23 日在Android中可以通过自定义主题风格方式来实现个性化以及复用,首先我们创建theme.xml主题文件,保存位置为工程的res/values/theme.xml ,这里我们可以可以为主题起一个名称,比如CWJ,这里去除了xml的文件头这行,我们在工程中只需在androidmanifest.xm...

weixin_34072003的博客 1674

2026实测可用!4个专业AI Agent Skills垂直搜索平台对比

📊本文以表格形式整理4个实测可用的专业AI Agent Skills垂直搜索平台,涵盖资源规模、适配生态等关键维度🤖,适合开发者按需选择技能获取渠道,解决平台筛选与访问难题✨

XiaoqiangClub的博客 525

android安卓的values的style和theme区别讲解和实际应用

样式 (Style)样式是指为 View 或窗口指定外观和格式的属性集合。样式可以指定高度、填充、字体颜色、字号、背景色等许多属性。 定义样式 要创建一组样式,请在您的项目的 res/values/ 目录中保存一个 XML 文件。 可任意指定该 XML 文件的名称,但它必须使用 .xml 扩展名,并且必须保存在 res/values/ 文件夹内, 该 XML 文件的根节点必须resources。(...

快乐李同学的博客 1812

LangChain .NET:C#开发者构建AI应用的RAG框架与实战指南

大语言模型(LLM)作为当前人工智能的核心技术,通过深度学习海量文本数据,掌握了强大的语言理解和生成能力。其原理基于Transformer架构,通过自注意力机制捕捉长距离依赖关系,从而实现对复杂语义的建模。这一技术价值在于能够将非结构化的自然语言转化为可计算、可推理的表示,为各类智能应用提供认知基础。在实际工程实践中,LLM常被应用于构建检索增强生成(RAG)系统和AI智能体(Agent)。RAG通过结合外部知识库检索与LLM生成,有效提升了问答的准确性和事实性,是构建企业知识库、智能客服等场景的关键技术。

weixin_42538175的博客 705

基于BQ25895RTWR的锂电池充电管理硬件设计

BQ25895 是一款适用于单节锂离子电池和锂聚合物电池的高度集成型 5A 开关模式电池充电管理和系统电源路径管理器件。此类器件支持高输入电压快速充电。低阻抗电源路径对开关模式运行效率进行了优化、缩短了电池充电时间并延长了放电阶段的电池使用寿命。• 移动电源、移动 Wi-Fi 热点。• 便携式互联网器件。

corlin6688的博客 1087

Android中Theme主题和Style样式使用介绍总结

一,Theme主题 1.1,Theme主题作用范围 Theme主要是针对应用级别的或者说窗体级别,可以设置应用主题(应用换肤效果)或者Activity的主题;主题是不能应用在某一个单独的View中; 1.2,Theme主题的定义 在AS中res/values/style.xml中定义,例如新建项目AS自动创建的Theme,是系统提供; <!-- Base application ...

ezconn的博客 2万+

【WRF工具】WRF Domain Wizard第一期:软件下载及安装

【WRF工具介绍】WRF Domain Wizard-确定模拟区域

WW、forever的博客 2918

安卓中的Theme和Style

写在最前面的话:Theme就是Style,只不过Style应用到Activity和Application中就不叫Style了,它叫Theme那么style和theme到底是什么,我们在实际的应用项目又该如何应用呢?不用着急,接下来和我一探究竟。

任锦明 407

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Android的theme和style

android的theme和style

qq_33464734的博客 681

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Android Theme和Style区别联系

sda

Caesardadi的专栏 4416

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Android 中style 和theme的描述题目

下面关于Android中定义style和theme的描述正确的是?A B D A 都可以减少重复属性设置 B style可以作用在Activity上 C Theme类可以继承 D 一个TextView的style中定义了textColor属性,TextView本身也设置textColor属性,那么TextView本身定义的优先级较高 解析: A 不提了 B的解释 官方文档定义如...

熊仙森滴Blog 1695

Android中Theme和Style的应用

Style:是一个包含一种或者多种格式化属性的集合,我们可以将其用为一个单位用在布局XML单个元素当中。比如,我们可以定义一种风格来定义文本的字号大小和颜色,然后将其用在View元素的一个特定的实例。Theme:是一个包含一种或者多种格式化属性的集合,我们可以将其为一个单位用在应用中所有的Activity当中或者应用中的某个Activity当中。比如,我们可以定义一个Theme,它为window...

weixin_30467087的博客 170

Android之Theme和Style

低级扩展方式为保持与平台界面样式的兼容性,你应始终通过扩展框架或支持库中的现有样式来创建自己的样式。如需扩展样式,请使用 parent 属性指定要扩展的样式。然后,你可以替换继承的样式属性并添加新属性。例如:</推荐扩展方式当然上述做法其实并不规范,官方建议我们直接继承支持库中的样式(已定义好相关的style)。不过,你应始终继承 Android 支持库中的核心应用样式。为与 Android 4.0(API 14)及更高版本兼容,支持库中的样式会针对各版本中可用的界面属性进行优化。

iceFreedom的博客 903

Android Attr、Style和Theme详解

关于Attr、Style和Theme详解 本文主要参考以下三篇博客的内容,感谢三位的分享 1.Attr、Style和Theme详解 2.一些关于style和attr的使用问题 3.Android中View自定义XML属性详解以及R.attr与R.styleable的区别 一:定义及基本使用方法 Attr:属性,风格样式的最小单元; Style:风格,它是一系列Attr的集合用以定义...

ifjgm003的博客 1114

Android 中的Theme和Style

Android 中的Theme和Style

欸奥维 1297

android学习之-Theme和Style

android style和theme的使用。 style:样式,用来描述单独的view控件,改变其样式。 theme:主题,用来控制整个应用的主题,或者某个activity的主题。 style的使用:改变某控件的样式。 在style.xml中描述样式信息 22sp #0000CC 在控件(TextVie

zhouguosen_always的专栏 2959

android -详谈Style和Theme

Android上的Style分为了两个方面: 1.Theme是针对窗体级别的,改变窗体样式 2.Style是针对窗体元素级别的,改变指定控件或者Layout的样式。 Android系统的themes.xml和style.xml(位于系统源代码frameworks\base\core\res\res\values\)包含了很多系统定义好的style 一些系统的Theme和St

li__haha的博客 318
上一篇: Android程序首次运行自动创建桌面快捷方式的方法
下一篇: Android-非常棒的HTTP通讯总结
Jymot
博客等级 码龄14年 6粉丝 15原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值