zoukankan      html  css  js  c++  java
  • Android的Style的使用

    Style个人理解就是view的一些属性的集合,那么一系列view(例如TextVIew),只要是要该style那么就都有相同的内容,如 文字的大少,颜色等,方便修改

    首先最基本的使用,多个textView都显示一样的颜色 跟文字大少等属性

       Sytle的定义:

    1. <style  name="TextViewStyle1">  
    2.       <item name="android:textColor">@android:color/holo_red_light</item>  
    3.       <item name="android:textSize">40sp</item>  
    4.       <item name="android:layout_height">wrap_content</item>  
    5.       <item name="android:layout_width">200dp</item>  
    6.       <item name="android:background">#ffff00ff</item>  
    7.       <item name="android:gravity">center_horizontal</item>  
    8.   </style>  

    这些属性都熟悉,使用

    1. <TextView   
    2.     style="@style/TextViewStyle1"  
    3.     android:layout_marginTop="100dp"  
    4.     android:text="test1"/>  
    5.    
    6. <TextView   
    7.     style="@style/TextViewStyle1"  
    8.      android:layout_marginTop="200dp"  
    9.     android:text="test2"/>  
    10. <TextView   
    11.     style="@style/TextViewStyle1"  
    12.      android:layout_marginTop="300dp"  
    13.     android:text="test3"/>  
    14. <TextView   
    15.     style="@style/TextViewStyle1"  
    16.      android:layout_marginTop="400dp"  
    17.     android:text="test4"/>  
    1. 那么结果就是<img src="http://img.blog.csdn.net/20140913101147703?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGV3ZW5jZTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />  
    1.   
    1. 那么我们在<TextView 中使用了Style 而且使用了与style中相冲突会早呢么样呢?  
    1. 修改第一个textView的背景跟颜色:  
    1. <pre name="code" class="html"><TextView   
    2.       style="@style/TextViewStyle1"  
    3.       android:layout_marginTop="100dp"  
    4.       android:gravity="center_horizontal"  
    5.       android:background="#ff00ff00"  
    6.       android:textColor="#ffffffff"  
    7.       android:text="test1"/>  

    那么结果就是

    1. 由此可以见,相关view的属性包括style中的所有的属性,view中自己还定义了的就使用view字定义的  
    1. style中的属性,在view中没有作用的会自动忽略掉  
    1.   

    2.style的继承

        1.加上parent

    <style name="TextViewStyle2" parent="@style/TextViewStyle1">
            <item name="android:layout_width">400dp</item>
        </style>

    2.加点

        <style name="TextViewStyle1.test">
            <item name="android:layout_width">800dp</item>
        </style>

      还可以多继承:

    <style name="TextViewStyle1.test.test">
            <item name="android:layout_width">1200dp</item>
        </style>

    那么布局文件改成:

      

    1. <TextView   
    2.       style="@style/TextViewStyle1"  
    3.       android:layout_marginTop="100dp"  
    4.       android:gravity="center_horizontal"  
    5.       android:background="#ff00ff00"  
    6.       android:textColor="#ffffffff"  
    7.       android:text="test1"/>  
    8.      
    9.   <TextView   
    10.       style="@style/TextViewStyle1.test.test"  
    11.        android:layout_marginTop="200dp"  
    12.       android:text="test2"/>  
    13.   <TextView   
    14.       style="@style/TextViewStyle2"  
    15.        android:layout_marginTop="300dp"  
    16.       android:text="test3"/>  
    17.   <TextView   
    18.       style="@style/TextViewStyle1.test"  
    19.       android:layout_marginTop="400dp"  
    20.       android:text="test4"/>  

    输出结果如下:


    sytle的更多属性见android包下的R.attr,这些都是系统的哦!

    使用Theme,这个就猛了,改了以后会影响真个程序的显示:

    系统默认有:

    1. <application  
    2.         android:allowBackup="true"  
    3.         android:icon="@drawable/ic_launcher"  
    4.         android:label="@string/app_name"  
    5.         android:theme="@style/AppTheme" >  

    我先把

    1. AppTheme修改一下吧:  
    1. 加入二个元素:<pre name="code" class="html"<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">  
    2.         <!-- API 14 theme customizations can go here. -->  
    3.         <strong><item name="android:textSize">60sp</item>  
    4.         <item name="android:typeface">monospace</item></strong>  
    5.     </style>  

    那么系统所有的文字的大少都是60sp,字体使用monospace(除非你们的View重新定义了) 更多的系统style可以在: sdkplatformsandrid-$APIdata es hemes.xm   styles.xml

    2.自定义属性的使用

    其实我们在style.xml中使用自定义属性的话,不需要写自定义控件的命名空间,我们只需要在style中使用命名控件的地方换成自定义控件的包名即可(注意:是包名,不带自定义控件的名字),如下:

      1. <resources xmlns:android="http://schemas.android.com/apk/res/android" >  
      2.     <style name="test" >  
      3.      <item name="android:textSize">60sp</item>
      4.         <item name="com.zhufuing:name_text">hello,world!</item>         
      5.     </style>  
      6. </resources
  • 相关阅读:
    Zepto swipe 无效(坑)
    Zepto.js-Ajax 请求
    Zepto.js-事件处理
    excel 大文件解析原理实现
    springboot 集成J2Cache
    springboot 单元测试 指定启动类
    springboot 解决 数字长度过长导致JS精度丢失问题
    JS 基本操作
    VUE 动态菜单管理
    VUE router-view key 属性解释
  • 原文地址:https://www.cnblogs.com/ldq2016/p/5226528.html
Copyright © 2011-2022 走看看