温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-06-25 10:54:51 来源:网络 阅读:11262 作者:zhf651555765 栏目:开发技术

   当我们做项目到一定程度的时候,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

官方图:

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

1.1.使用

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

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

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

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

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

  A.应用到Application

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

   B.应用到Activity

<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简单使用

   一个简单地样式

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

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

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

2.2定义风格

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

<?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! 就是这些了!




向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI