无涯教程-Android - Theme Demo Example函数

236 阅读4分钟

下面的示例演示如何在应用程序中使用主题。出于演示目的,我们将修改默认的 AppTheme ,其中默认文本,其大小,系列,阴影等将被更改。让我们开始按照以下步骤创建一个简单的Android应用程序-

步骤 说明
1 您将使用Eclipse IDE创建一个Android应用程序,并在 com.example.themedemo 包下将其命名为 ThemeDemo ,如 Hello WorldExample中所述章。
2 修改 src/MainActivity.java 文件,为定义的两个按钮添加单击事件侦听器和处理程序。
3 在全局样式文件 res/values/style.xml 中定义样式,以定义按钮的自定义属性,并更改应用程序的默认主题以与文本一起播放。
4 修改 res/layout/activity_main.xml 文件的检测内容,以包括一组Android UI控件并使用已定义的样式。
5 在 res/values/strings.xml 文件中定义所需的常量
6 运行应用程序以启动Android模拟器并验证应用中所做更改的输出。

以下是修改后的主要活动文件 src/com.example.themedemo/MainActivity.java 的内容。该文件可以包括每个基本生命周期方法。

package com.example.themedemo;

import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView;

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

  //--- find both the buttons---
  Button sButton=(Button) findViewById(R.id.button_s);
  Button lButton=(Button) findViewById(R.id.button_l);
  
  //-- register click event with first button ---
  sButton.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
        //--- find the text view --
        TextView txtView=(TextView) findViewById(R.id.text_id);
        
        //-- change text size --
        txtView.setTextSize(20);
     }
  });
  
  //-- register click event with second button ---
  lButton.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
        //--- find the text view --
        TextView txtView=(TextView) findViewById(R.id.text_id);
        
        //-- change text size --
        txtView.setTextSize(24);
     }
  });

}

@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }

以下是 res/values/style.xml 文件的内容,该文件的附加样式 CustomButtonStyle 已定义-

<resources>

<!-- 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="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> <item name="android:capitalize">characters</item> <item name="android:typeface">monospace</item> <item name="android:shadowDx">1.2</item> <item name="android:shadowDy">1.2</item> <item name="android:shadowRadius">2</item> <item name="android:textColor">#494948</item>/> <item name="android:gravity" >center</item> <item name="android:layout_margin" >3dp</item> <item name="android:textSize" >5pt</item> <item name="android:shadowColor" >#000000</item> </style>

<!-- Custom Style defined for the buttons. --> <style name="CustomButtonStyle"> <item name="android:layout_width">100dp</item> <item name="android:layout_height">38dp</item> </style>

</resources>

以下是 res/layout/activity_main.xml 文件的内容-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical">

<Button android:id="@+id/button_s" style="@style/CustomButtonStyle" android:text="@string/button_small" android:onClick="doSmall"/>

<Button android:id="@+id/button_l" style="@style/CustomButtonStyle" android:text="@string/button_large" android:onClick="doLarge"/>

<TextView android:id="@+id/text_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:capitalize="characters" android:text="@string/hello_world" />

</LinearLayout>

以下是 res/values/strings.xml 的内容,以定义两个新的常量-

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">ThemeDemo</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello world!</string>
   <string name="button_small">Small Font</string>
   <string name="button_large">Large Font</string>
</resources>

以下是 AndroidManifest.xml 的默认内容。在这里,我们无需更改任何内容,因为我们保持主题名称不变。但是,如果您定义了新的新主题或使用其他名称继承了默认主题,则必须用新的主题名称替换 AppTheme 名称。

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

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

<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >

  &lt;activity
     android:name="com.example.guidemo.MainActivity"
     android:label="@string/app_name" &gt;
        
     &lt;intent-filter&gt;
        &lt;action android:name="android.intent.action.MAIN" /&gt;
        &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
     &lt;/intent-filter&gt;
     
  &lt;/activity&gt;
  

</application> </manifest>

让我们尝试运行您的 ThemeDemo 应用程序。我假设您在进行环境设置时创建了 AVD 。要从Eclipse运行该应用程序,请打开您项目的活动文件之一,然后从菜单栏中单击运行 Eclipse Run Icon图标工具栏。 Eclipse将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示在Emulator窗口下面-

Android Custom Theme

参考链接

www.learnfk.com/android/and…