Android 基础知识

Android 主页 Android 概述 Android 下载安装和开发环境配置 Android Studio IDE Android 架构 Android 应用程序组件 Android Hello World 示例 Android 资源 Android 活动 Android 服务 Android 广播接收器 Android 内容提供者 Android 片段 Android Intents/Filters

Android 用户界面

Android UI 布局 Android UI 控件 Android 事件处理 Android 样式和主题 Android 自定义组件

Android 高级概念

Android 拖放 Android 通知 Android 基于位置的服务 Android 发送电子邮件 Android 发送短信 Android 拨打电话 Android 发布应用程序

Android 实用示例

Android 警报对话框 Android 动画 Android 音频捕捉 Android 音频管理器 Android 自动完成 Android 最佳实践 Android 蓝牙 Android 相机 Android 剪贴板 Android 自定义字体 Android 数据备份 Android 开发者工具 Android 模拟器 Android Facebook 集成 Android 手势 Android 谷歌地图 Android 图像效果 Android 图像切换 Android 内部存储 Android JetPlayer Android JSON 解析器 Android Linkedin 集成 Android 旋转加载器 Android 本地化 Android 登录应用 Android 媒体播放器 Android 多点触控 Android 导航 Android 网络连接 Android NFC 指南 Android PHP/MySQL Android 进度圈 Android 进度条 Android 推送通知 Android 渲染脚本 Android RSS 阅读器 Android 屏幕投射 Android SDK 管理器 Android 传感器 Android 会话管理 Android 共享首选项 Android SIP 协议 Android 拼写检查器 Android SQLite 数据库 Android 支持库 Android 测试 Android 文字转语音 Android TextureView Android Twitter 集成 Android UI 设计 Android UI 模式 Android UI 测试 Android WebView 布局 Android Wi-Fi Android Widgets Android XML 解析器

Android 其他

Android 面试问题 Android 有用的资源 Android 测验


Android - Theme Demo Example

Following example demonstrates how you can use a theme for an application. For demo purpose we will modify our default AppTheme wehere default text, its size, family, shadow etc will be changed. Let's start with creating a simple Android application as per the following steps −

步骤 描述
1 You will use Eclipse IDE to create an Android application and name it as ThemeDemo under a package com.example.themedemo as explained in the Hello World Example chapter.
2 Modify src/MainActivity.java file to add click event listeners and handlers for the two buttons defined.
3 Define your style in global style file res/values/style.xml to define custom attributes for a button and change default theme of the application to play with the text.
4 Modify the detault content of res/layout/activity_main.xml file to include a set of Android UI controls and make use of the defined style.
5 Define required constants in res/values/strings.xml file
6 Run the application to launch Android emulator and verify the result of the changes done in the aplication.

Following is the content of the modified main activity file src/com.example.themedemo/MainActivity.java. This file can include each of the fundamental lifecycle methods.

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;
   }
}

Following will be the content of res/values/style.xml file which will have addition style CustomButtonStyle defined −

<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>

Following will be the content of res/values/strings.xml to define two new constants −

<?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>

Following is the default content of AndroidManifest.xml. Here we do not need to change anything because we kept out theme name unchanged. But if you define fresh new theme or inherit a default them with different name then you will have to replace AppTheme name with the new them name.

<?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" >
      
      <activity
         android:name="com.example.guidemo.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>

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

Android 自定义主题

❮ Android 样式和主题