Style中文应该是式样,可以使用它指定View的外观,程序的主题等。下面是示例:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:background="#ff00ffff"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textColor="#ffff0000"
- android:textSize="18sp"
- android:text="Hello android"
- />
- </LinearLayout>
本来我们定义一个View对象是以这种方式进行的,现在我们应用Style。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- style="@style/buttonstyle"
- android:text="Hello android"
- />
- </LinearLayout>
在res/velues目录下面新建一个style.xml的文件。文件名是任意的,不过必须要以.xml结尾。
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="buttonstyle" parent="@android:style/TextAppearance.Medium">
- <item name="android:background">#ff00ffff</item>
- <item name="android:layout_width">fill_parent</item>
- <item name="android:layout_height">wrap_content</item>
- <item name="android:textColor">#ff00ffff</item>
- <item name="android:textSize">18sp</item>
- </style>
- </resources>
这两种定义Button的方式实现的效果是一样的。经过比较就可以知道Style'的使用方法。需要注意的是 parent 指定了式样的继承。Android预定义了一些式样,通过合理的继承可以减轻我们的工作。上面的方式是继承Android为我们提供的式样,要继承自己定 义的式样,方法稍有不同。
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="MyButton.buttonstyle">
- .............
- </style>
- </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应用整个程序。
- <application android:icon="@drawable/icon"
- android:label="@string/app_name"
- android:theme="@style/buttonstyle"
- >
当然一个程序可能有多个Activity,我们可以分别为他们自定主题。下面的列子让我们的程序透明起来。
- public class StyleTestActivity extends Activity implements OnClickListener{
- /** Called when the activity is first created. */
- Button button ;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button)findViewById(R.id.button);
- button.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Toast toast = Toast.makeText(getApplicationContext(), "hello android", Toast.LENGTH_SHORT);
- toast.show();
- }
- }
下面内容在res/velus/style.xml中
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <color name="transparent_background">#44004400</color>
- <style name="transparent">
- <item name="android:windowBackground">@color/transparent_background</item>
- <item name="android:windowNoTitle">true</item>
- <item name="android:windowIsTranslucent">true</item>
- <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
- </style>
- </resources>
在AndroidManifest.xml中指定主题:
-
- <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>