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

1.<?xmlversion="1.0"encoding="utf-8"?>

2.<LinearLayoutxmlns: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.<Button8.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="Helloandroid"14./>15.</LinearLayout>本来我们定义一个View对象是以这种方式进行的,现在我们应用Style。

1.<?xmlversion="1.0"encoding="utf-8"?>

2.<LinearLayoutxmlns: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.<Button8.style="@style/buttonstyle"9.Android:text="Helloandroid"10./>11.</LinearLayout>在res/velues目录下面新建一个style.xml的文件。文件名是任意的,不过必须要以.xml结尾。

1.<?xmlversion="1.0"encoding="utf-8"?>

2.<resources>3.<stylename="buttonstyle"parent="@Android:style/TextAppearance.Medium">4.<itemname="Android:background">#ff00ffff</item>5.<itemname="Android:layout_width">fill_parent</item>6.<itemname="Android:layout_height">wrap_content</item>7.<itemname="Android:textColor">#ff00ffff</item>8.<itemname="Android:textSize">18sp</item>9.</style>10.</resources>

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

1.<?xmlversion="1.0"encoding="utf-8"?>

2.<resources>

2.<stylename="MyButton.buttonstyle">3..............

4.</style>5.</resources>

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

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

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

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

1.<applicationAndroid:icon="@drawable/icon"

2.Android:label="@string/app_name"3.Android:theme="@style/buttonstyle"4.>当然一个程序可能有多个Activity,我们可以分别为他们自定主题。下面的列子让我们的程序透明起来。

1.publicclassStyleTestActivityextendsActivityimplementsOnClickListener{

2./**Calledwhentheactivityisfirstcreated.*/3.Buttonbutton;

[email protected](BundlesavedInstanceState){6.super.onCreate(savedInstanceState);7.setContentView(R.layout.main);

8.button=(Button)findViewById(R.id.button);

9.button.setOnClickListener(this);10.}

[email protected](Viewv){13.//TODOAuto-generatedmethodstub14.Toasttoast=Toast.makeText(getApplicationContext(),"helloAndroid",Toast.LENGTH_SHORT);15.toast.show();

16.}

17.}

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

1.<?xmlversion="1.0"encoding="utf-8"?>

2.<resources>

3.<colorname="transparent_background">#44004400</color>4.<stylename="transparent">5.<itemname="Android:windowBackground">@color/transparent_background</item>6.<itemname="Android:windowNoTitle">true</item>7.<itemname="Android:windowIsTranslucent">true</item>8.<itemname="Android:windowAnimationStyle">@+android:style/Animation.Translucent</item>9.</style>

10.</resources>

在AndroidManifest.xml中指定主题:

1.2.<activityAndroid:name=".StyleTestActivity"

3.

•Android:label="@string/app_name"

•Android:theme="@style/transparent">•<intent-filter>•<actionAndroid:name="android.intent.action.MAIN"/>•<categoryAndroid:name="android.intent.category.LAUNCHER"/>•</intent-filter>•</activity>布局mian.xml就自己写吧……