`
sunshangfeng
  • 浏览: 11798 次
  • 性别: Icon_minigender_1
文章分类
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
Android :Style的使用
Style中文应该是式样,可以使用它指定View的外观,程序的主题等。下面是示例:

1.<?xml version="1.0" encoding="utf-8"?>  
2.<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/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="http://schemas.android.com/apk/res/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">#ff00ffff</item>  5.        <item name="Android:layout_width">fill_parent</item>  6.        <item name="Android:layout_height">wrap_content</item>  7.        <item name="Android:textColor">#ff00ffff</item>  8.        <item name="Android:textSize">18sp</item>  9.    </style>  10.</resources>  
这两种定义Button的方式实现的效果是一样的。经过比较就可以知道Style'的使用方法。需要注意的是parent 指定了式样的继承。Android预定义了一些式样,通过合理的继承可以减轻我们的工作。上面的方式是继承Android为我们提供的式样,要继承自己定义的式样,方法稍有不同。

1.<?xml version="1.0" encoding="utf-8"?>  
2.<resources>  
2.    <style name="MyButton.buttonstyle">  3.     .............  
4.    </style>  5.</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.<?xml version="1.0" encoding="utf-8"?>  
2.<resources>  
3.    <color name="transparent_background">#44004400</color>  4.    <style name="transparent">  5.       <item name="Android:windowBackground">@color/transparent_background</item>  6.       <item name="Android:windowNoTitle">true</item>  7.       <item name="Android:windowIsTranslucent">true</item>  8.       <item name="Android:windowAnimationStyle">@+android:style/Animation.Translucent</item>  9.     </style>  
10.</resources>  
在AndroidManifest.xml中指定主题:

1.2.<activity Android:name=".StyleTestActivity"  
3.
•                  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>  布局mian.xml就自己写吧……
Global site tag (gtag.js) - Google Analytics