【移动开发】Android中Theme和Style的使用

简介:

 当我们做项目到一定程度的时候,Android提供给我们的简单的UI控件(虽然后期ADT插件更新中增加了不少)已经不能满足我们的需要,一款成功的软件不仅要功能强大,漂亮的界面同样会吸引不少用户!

   这里,我将总结一下Android提供给我们的两个重要的资源:Theme 和 Style。


1.Theme(是针对窗体级别的,可以改变窗体样式)

官方文档  

       Themes are Android's mechanism for applying a consistent style to an app or activity. The style specifies the visual properties of the elements that make up your user interface, such as color, height, padding and font size. To promote greater cohesion between all apps on the platform, Android provides three system themes that you can choose from when building apps for Ice Cream Sandwich:

      主题是Android的机制被应用一个相同风格的应用程序Activity中。样式指定了视觉属性的元素装饰你的用户界面,如颜色、高度、填充和字体大小。促进更大的凝聚力在所有平台的应用程序,Android提供了三个系统的主题,您可以选择在构建应用程序选择不同的主题:

   Holo Light
   Holo Dark
   Holo Light with dark action bars

官方图:

170651183.png170651260.png170651145.png

1.1.使用

   Android系统的themes.xml和style.xml(位于/base/core/res/res/values/)包含了很多系统定义好的style,建议在里面挑个合适的,然后再继承修改。以下属性是在Themes中比较常见的,源自Android系统本身的themes.xml:

1
2
3
4
5
6
7
8
< style
    name = "simple_dialog"  parent = "@android:style/Theme.Dialog" >
        < item  name = "android:windowFrame" >@null</ item > <!-- Dialog的WindowFrame框为无 -->
        < item  name = "android:windowNoTitle" >true</ item >
        < item  name = "android:windowIsFloating" >true</ item > <!-- 是否悬浮在activity上 -->
        < item  name = "android:windowIsTranslucent" >true</ item > <!-- 是否半透明 -->
        < item  name = "android:backgroundDimEnabled" >false</ item > <!-- 背景是否模糊 -->
    </ style >

有人会问从哪里学习这些主题?其实系统就自带了很多,看下图:

171730366.jpg

在AndroidManifest的Application中,点击Theme选项,系统资源里就有相当多的主题供你选择

  A.应用到Application

1
<application android:theme= "@style/CustomTheme" >

   B.应用到Activity

1
<activity android:theme= "@android:style/Theme.Dialog" >


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

 官方文档:

  A 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.

    风格在安卓中的份额就如同级联样式表在web设计中的设计,允许你将内容分割开来设计

   2.1简单使用

   一个简单地样式

1
2
3
4
5
6
< TextView
     android:layout_width = "fill_parent"
     android:layout_height = "wrap_content"
     android:textColor = "#00FF00"
     android:typeface = "monospace"
     android:text = "@string/hello"  />

    可以在任何相同的地方被引用

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

2.2定义风格

   首先可以在res/values/下创建一个xml,用于定义style(注意:根结点必须是<resources>

1
2
3
4
5
6
7
8
9
<? xml  version = "1.0"  encoding = "utf-8" ?>
< resources >
     < style  name = "CodeFont"  parent = "@android:style/TextAppearance.Medium" >
         < item  name = "android:layout_width" >fill_parent</ item >
         < item  name = "android:layout_height" >wrap_content</ item >
         < item  name = "android:textColor" >#00FF00</ item >
         < item  name = "android:typeface" >monospace</ item >
     </ style >
</ resources >

   这里我们可以看到,该style的名称为CodeFont,parent后面是父类的style,(当然,我们也可以不用继承父类style),接下来每一个item定义一个属性。定义属性的最好方法就是在api文档里找到这个view的xml属性,比如在EditText中有InputType这个属性,那么在你的style里面你就可以来定义它。


   ok! 就是这些了!



     本文转自zhf651555765 51CTO博客,原文链接:http://blog.51cto.com/smallwoniu/1251507 ,如需转载请自行联系原作者



相关文章
|
4月前
|
Android开发 开发者
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
44 1
|
Android开发
Android系统自带样式(android:theme)解析
做Android开发时经常会修改系统默认的主题样式,在android的sdk  安装目录data\res\values\themes.
2388 0
|
10月前
|
API Android开发 UED
|
XML 移动开发 ARouter
为什么说Flutter让移动开发变得更好?Android篇
为什么说Flutter让移动开发变得更好?Android篇
为什么说Flutter让移动开发变得更好?Android篇
|
Web App开发 移动开发 前端开发
Android面试:为什么说Flutter让移动开发变得更好?全套教学资料
Android面试:为什么说Flutter让移动开发变得更好?全套教学资料
|
API Android开发
Android中的Style、Theme详解已经发展史
版权声明:本文为sydMobile原创文章,转载请务必注明出处! https://blog.csdn.net/sydMobile/article/details/80164916 Style介绍 style就像单词意思一样,风格,这里面是属性的集合,如果页面中有许多控件的属性值相同那么就可以把这些属性抽出来放到style里面,定义也很简单,在values文件下的styles里面创建就可以了。
1632 0
|
Android开发 Java 开发工具
|
Android开发
Android中style和theme的区别
在学习Xamarin android的过程中,最先开始学习的还是熟练掌握android的六大布局-LinearLayout 、RelativeLayout、TableLayout、FrameLayout、GridLayout、AbsoluteLayout。
863 0