Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1250142
  • 博文数量: 247
  • 博客积分: 5587
  • 博客等级: 大校
  • 技术积分: 2060
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-24 13:27
文章分类
文章存档

2012年(101)

2011年(44)

2010年(102)

分类: 嵌入式

2012-03-23 14:11:53

Style中文应该是式样,可以使用它指定View的外观,程序的主题等。下面是示例:

  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android=""  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <Button  
  8.     android:background="#ff00ffff"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:textColor="#ffff0000"  
  12.     android:textSize="18sp"  
  13.     android:text="Hello android"  
  14.     />  
  15. LinearLayout>  

本来我们定义一个View对象是以这种方式进行的,现在我们应用Style。

  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android=""  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <Button  
  8.     style="@style/buttonstyle"  
  9.     android:text="Hello android"  
  10.     />  
  11. LinearLayout>  

在res/velues目录下面新建一个style.xml的文件。文件名是任意的,不过必须要以.xml结尾。

  1. xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="buttonstyle" parent="@android:style/TextAppearance.Medium">  
  4.         <item name="android:background">#ff00ffffitem>  
  5.         <item name="android:layout_width">fill_parentitem>  
  6.         <item name="android:layout_height">wrap_contentitem>  
  7.         <item name="android:textColor">#ff00ffffitem>  
  8.         <item name="android:textSize">18spitem>  
  9.     style>  
  10. resources>  

这两种定义Button的方式实现的效果是一样的。经过比较就可以知道Style'的使用方法。需要注意的是 parent 指定了式样的继承。Android预定义了一些式样,通过合理的继承可以减轻我们的工作。上面的方式是继承Android为我们提供的式样,要继承自己定 义的式样,方法稍有不同。

  1. xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="MyButton.buttonstyle">  
  4.      .............  
  5.     style>  
  6. resources>  

它说明buttonstyle继承于MyButton式样,而MyButton是一个自己定义的式样。这种方式是可 以递归的。你可以另外定义一个substyle式样让他继承自buttonstyle,那么就是 MyButton.buttonstyle.substyle了。

tips:我们为一个View指定了style,那么它会应用这个style。如果我们为一个ViewGroup指 定了式样,那么它的Child View是不会继承它的style的。想要让所有的视图都继承一个Style,那就要用到主题Theme了(这句话虽然源自文档,但是强烈质疑这种做法的 有效性)。


前面提到了主题Theme这个话题。我们先看看如何将整个Application应用主题。

在AndroidManifest.xml文件中有个application的子节点,可以添加我们的style应用整个程序。

  1. <application android:icon="@drawable/icon"   
  2.              android:label="@string/app_name"  
  3.              android:theme="@style/buttonstyle"  
  4.              >  

当然一个程序可能有多个Activity,我们可以分别为他们自定主题。下面的列子让我们的程序透明起来。

  1. public class StyleTestActivity extends Activity implements OnClickListener{  
  2.     /** Called when the activity is first created. */  
  3.     Button button ;  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.         button = (Button)findViewById(R.id.button);  
  9.         button.setOnClickListener(this);  
  10.     }  
  11.     @Override  
  12.     public void onClick(View v) {  
  13.         // TODO Auto-generated method stub   
  14.         Toast toast = Toast.makeText(getApplicationContext(), "hello android", Toast.LENGTH_SHORT);  
  15.         toast.show();  
  16.     }  
  17. }  

下面内容在res/velus/style.xml中

  1. "1.0" encoding="utf-8"?>  
  2.   
  3.     "transparent_background">#44004400  
  4.     "transparent">  
  5.        "android:windowBackground">@color/transparent_background  
  6.        "android:windowNoTitle">true  
  7.        "android:windowIsTranslucent">true  
  8.        "android:windowAnimationStyle">@+android:style/Animation.Translucent  
  9.        
  10.   

在AndroidManifest.xml中指定主题:


  1. <activity android:name=".StyleTestActivity"  

  •                   android:label="@string/app_name"  
  •                   android:theme="@style/transparent">  
  •             <intent-filter>  
  •                 <action android:name="android.intent.action.MAIN" />  
  •                 <category android:name="android.intent.category.LAUNCHER" />  
  •             intent-filter>  
  •         activity> 
  • 阅读(6059) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~