android中的syle和theme

来源:互联网 发布:优衣库网站的seo分析 编辑:程序博客网 时间:2024/05/16 12:59

1、Style 风格、样式,是针对View控件的,是一系列属性的集合,用于简化代码编写。是元素级别的属性设置

1)引入,例在layout中设置多个控件View1, View2, ... , Viewn

      如果这些控件都有相同的某些属性值,如

      android:textSize = 10sp; 

     android:background= "#ffffff";  

     android:textColor = "#000000";

     android:layout_width = "200dp"

2)现在有n个控件,上述设置是不是要输入n次呢?而且修改、维护相当的不便、且容易出错。

3)引入style,让后让每个View引用此Style

     在res\values文件下,创建Style的xml文件,此处命名为myStyle.xml

    <resouces>

           <style>

                <item name="android:textSize">10sp</item>

               <item name="android:background">#ffffff</item>

              <item name="android:textColor ">#000000</item>

              <item name="android:layout_width">200dp</item>

           </style>

    </resouces>

4)使用Style, style = " @style/myStyle "


2、Theme主题,用于设置应用程序或Activity的属性,是针对页面级别的。建立Theme设置的属性文档的方法与style相同。

1)自定义Theme,需要在xml文档中设置通用属性。此处和设置Style类似。

2)引用Theme,需要在应用程序描述AndroidManifest.xml中添加,

如: <application  android:theme=" @style/CustomStyle ">

         </application>

通过代码设置主题setTheme(android.R.style.CustomStyle);

必须在设置VIew之前,即在setContentView(xxxx)前。




0 0