安卓专题

安卓样式Style和主题Theme案例源码

  Android的样式是一个指定格式的XML资源,通过声明一些特性,如颜色,边距,字体大小等,它已经从XML布局Layout中分离出来。可以说相当于是Android的CSS,因为它将设计从内容中分离。主题Themes无非是适用于整个应用程序或特定活动的样式。

  为了创建一个样式,首先需要在res/values目录下创建一个XML文件,其中有一个根元素 <resource> 样式标签使用<style>,它包含多个<item>元素来声明样式的属性。指定样式的名称非常重要,这是它唯一标识符。

  Android也提供了继承的样式。样式可以继承的现有样式或默认Android风格的一些属性,然后可以添加或更改其中的一些。有两种方式继承:第一个使用<parent>元素,在此我们指定要继承的属性的样式名称。另外一个,使用该样式的名称,我们通过后跟一个点前缀(. )实现继承。重要的是第二种方法仅用于我们自己定义的样式。如果我们要继承一个内置Android风格,强制使用<parent>元素。

  创建的res/values/JCG_style.xml文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
   
    <style name="SimpleStyle" parent="@android:style/TextAppearance">
      <item name="android:typeface">monospace</item>
      <item name="android:textSize">11pt</item>
      <item name="android:textColor">#000000</item>
    </style>
   
</resources>

这里样式继承了内置风格extAppearance,包含三个简单属性。

下面再创建第二种继承样式,创建XML文件res/values/inherit_style.xml 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="SimpleStyle.BigPurple">
      <item name="android:textSize">16pt</item>
      <item name="android:textColor">#FF00FF</item>
   </style>
</resources>

这里SimpleStyle是我们自己定义的新样式,这里被继承。

创建一个应用的主题

  为了定义主题,打开 res/values/styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="MyAppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
      <item name="android:typeface" >serif</item>
       <item name="android:textColor">#000000</item>
       <item name="android:textSize" >13pt</item>
      <item name="android:shadowDx" >1.5</item>
       <item name="android:shadowDy" >1.5</item>
       <item name="android:shadowRadius" >1</item>
       <item name="android:shadowColor" >#AAAAAA</item>
       <item name="android:gravity" >center</item>
    </style>
   
    <style name="ButtonStyle">
      <item name="android:layout_width" >300dp</item>
      <item name="android:layout_height" >wrap_content</item>
      <item name="android:layout_marginTop" >10dp</item>
      <item name="android:textSize" >10pt</item>
      <item name="android:background" >#FF0000</item>
    </style>

</resources>

这里定义主题名是MyAppTheme 和<parent>根元素,这是从已有的主题中继承,也定义了样式名为ButtonStyle,包括一些属性,还定义了Button的主题,这里没有使用<parent>,因为是可选的。

下面是应用这个主题,这是通过 AndroidManifest.xml 完成,表示整个应用都是使用这个主题:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jdon.android.stylesthemestest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/MyAppTheme" >
        <activity
            android:name="com.jdon.android.stylesthemestest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

在application配置中,指定了样式为我们之前定义的MyAppTheme,为了设置样式,还要加上活动的配置。

创建活动布局

  下面是创建一个活动的布局Layout,我们使用一个简单的布局,打开res/layout/activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/customText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text 1..." />
   
    <TextView
        android:id="@+id/bigText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/customText"
        android:text="Text 2..." />
   
    <Button
        android:id="@+id/simpleTextButton"
        android:layout_below="@id/bigText"
        style="@style/ButtonStyle"
        android:text="SimpleStyle for Text 1" />
   
    <Button
        android:id="@+id/bigTextButton"
        android:layout_below="@id/simpleTextButton"
        style="@style/ButtonStyle"
        android:text="BigPurpleStyle for Text 2" />

</RelativeLayout>

在这个布局中,我们也是有了我们定义的样式ButtonStyle

下面编制主活动src/com.jdon.android.stylesthemestest/MainActivity.java 

public class MainActivity extends Activity {

   private Button simpleBtn, bigBtn;
   private TextView text1, text2;
  
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
     
      text1 = (TextView) findViewById(R.id.customText);
      text2 = (TextView) findViewById(R.id.bigText);
     
      simpleBtn = (Button) findViewById(R.id.simpleTextButton);
      simpleBtn.setOnClickListener(new OnClickListener() {
        
         @Override
         public void onClick(View arg0) {
            // TODO Auto-generated method stub
            text1.setTextAppearance(getApplicationContext(), R.style.SimpleStyle);
         }
      });
     
      bigBtn = (Button) findViewById(R.id.bigTextButton);
      bigBtn.setOnClickListener(new OnClickListener() {
        
         @Override
         public void onClick(View arg0) {
            // TODO Auto-generated method stub
            text2.setTextAppearance(getApplicationContext(), R.style.SimpleStyle_BigPurple);
         }
      });
   }

}

运行效果如下:

 

本案例源码下载