样式表以及Color.xml文件『Android系列六』

来源:互联网 发布:广告公司用到的软件 编辑:程序博客网 时间:2024/05/21 05:40

        上篇我们知道了怎么改变TextView标签的各种属性,问题是,如果页面上有几十个上百个同类标签,难道要一个一个的修改吗?马上想到了CSS样式表,这里要庆幸的是Android同样支持样式表的加载。

        简单来说只涉及两类文件:layout/main.xml、values/style.xml,下面是他们各自的代码:

        main.xml

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <TextView  
  7.         style="@style/firstStyle"  
  8.         android:id="@+id/firstText"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/hello_world" />  
  12.      
  13. </LinearLayout>  

        style.xml
[html] view plaincopy
  1. <resources xmlns:android="http://schemas.android.com/apk/res/android">  
  2.   
  3.     <style name="firstStyle">  
  4.         <item name="android:textSize">18sp</item>  
  5.         <item name="android:textColor">#FF00FF</item>  
  6.     </style>  
  7.   
  8. </resources>  

        为了更清晰的证明样式表作用,把main.java代码也贴出来
[java] view plaincopy
  1. package com.dy.study.firstbase;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5.   
  6. public class Main extends Activity {  
  7.   
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.     }  
  13. }  

        可以注意到,完全没有改变,那么看看效果吧。



      再对比一下,把样式表的颜色改为#00FFFF,再看下效果:


       样式表简单介绍到这里,接着说color.xml,由于系统提供的Color.GREEN等之类的太少太单薄,所以为了美化UI,还是自己定义多个颜色代码方便使用吧,至于color.xml里面的内容,网上一搜一堆,我在学习的时候就是借鉴其他人共享的资料,这里感谢一下分享者,言归正传,简单列举几个颜色代码,并在main.java里面调用,看看效果:

       color.xml

[html] view plaincopy
  1. <resources>  
  2.   
  3.     <color name="pink">#ffc0cb</color>   <!-- 粉红色 -->  
  4.     <color name="white">#ffffff</color>   <!-- 白色 -->  
  5.     <color name="gold">#ffd700</color>   <!-- 金色 -->  
  6.     <color name="indianred">#cd5c5c</color>   <!-- 印第安红 -->  
  7.     <color name="mediumvioletred">#c71585</color>   <!-- 中紫罗兰色 -->  
  8.   
  9. </resources>  

       main.java
[java] view plaincopy
  1. package com.dy.study.firstbase;  
  2.   
  3. import android.os.Bundle;  
  4. import android.widget.Button;  
  5. import android.widget.TextView;  
  6. import android.app.Activity;  
  7.   
  8. public class Main extends Activity {  
  9.   
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.main);  
  14.           
  15.         findViews();  
  16.           
  17.         change();  
  18.     }  
  19.       
  20.     private TextView firstText;  
  21.     private Button firstButton;  
  22.       
  23.     private void findViews(){  
  24.         firstText=(TextView)findViewById(R.id.firstText);  
  25.         firstButton=(Button)findViewById(R.id.firstButton);  
  26.     }  
  27.     private void change(){  
  28.         firstText.setTextColor(getResources().getColor(R.color.gold));  
  29.         firstButton.setTextColor(getResources().getColor(R.color.indianred));  
  30.     }     
  31. }  

        main.xml

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <TextView  
  7.         android:id="@+id/firstText"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello_world" />  
  11.   
  12.     <Button  
  13.         android:id="@+id/firstButton"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="@string/demo" />  
  17.   
  18. </LinearLayout>  


        然后看看显示结果:


        这里需要注意的是,获取颜色信息需要使用getResources().getColor(R.color.gold)格式。样式表和color文件说完了,愿大家都有一个多彩靓丽的生活。

0 0
原创粉丝点击