Android開發教學
Android 開發環境配置
Android 架構
Android 應用組件
Android Hello World示例
Android 資源組織和訪問
Android Activity
Android Service
Android廣播接收器
Android內容提供者
Android碎片/片段
Android Intent過濾器
Android UI佈局
Android LinearLayout
Android RelativeLayout
Android TableLayout
Android AbsoluteLayout
Android FrameLayout
Android ListView
Android GridView
Android UI控件
Android TextView
Android EditText
Android AutoCompleteTextView
Android Button
Android ImageButton
Android CheckBox
Android ToggleButton
Android RadioButton
Android RadioGroup
Android事件處理
Android樣式和主題
Android樣式示例
Android主題示例
Android自定義組件
Android拖放
Android通知
Android基於位置服務
Android發送電子郵件
Android發送短信/SMS
Android撥打電話
發佈Android應用
ArrayAdapter
SimpleCursorAdapter
Android ProgressDialog
Android Spinner
使用活動代碼自定義Android組件
使用佈局文件自定義Android組件
Android自定義組件及屬性
Android Alertdialog(警告對話框)
Android Animation(動畫)實例
Android音頻捕獲(錄音)
Android音頻管理器實例
Android AutoCompleteTextView(自動完成)實例
Android最佳實踐
Android Bluetooth(藍牙)實例
Android Camera(攝像頭)
Android Clipboard(複製/剪貼板)
Android自定義字體
Android數據備份
Android Gestures/手勢
Android圖片效果
Android圖片切換
Android內部存儲
Android JetPlayer實例
Android JSON解析器
Android加載Spinner
Android本地化
Android登錄實例
Android MediaPlayer(多媒體播放)

Android主題示例

下面的例子演示如何使用的應用程序的主題(Theme)。修改默認 AppTheme 的默認文本,改變它的大小,系列,陰影等。現在開始創建一個簡單的Android應用程序按照以下步驟:

步驟

描述

1

使用Android Studio創建一個Android應用程序,並將其命名爲:ThemeDemo

2

修改 src/MainActivity.java 文件,以添加click事件偵聽器和處理程序定義的兩個按鈕

3

定義樣式風格在全局樣式文件  res/values/style.xml  定義應用程序裝飾按鈕的文本,改變默認主題自定義屬性

4

修改 res/layout/activity_main.xml 文件的默認內容包括一套Android的UI控件,並使用已定義樣式

5

定義 res/values/strings.xml  文件所需的常量

6

運行該應用程序啓動 Android模擬器並驗證應用程序所做的修改結果

以下是主活動文件 src/com.yiibai.guidemo9/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 定義的內容:

<style name="AppBaseTheme" parent="android:Theme.Light"> <style name="AppTheme" parent="AppBaseTheme"> <item name="android:capitalize">characters <item name="android:typeface">monospace <item name="android:shadowDx">1.2 <item name="android:shadowDy">1.2 <item name="android:shadowRadius">2 <item name="android:textColor">#494948/> <item name="android:gravity" >center <item name="android:layout_margin" >3dp <item name="android:textSize" >5pt <item name="android:shadowColor" >#000000 <style name="CustomButtonStyle"> <item name="android:layout_width">100dp <item name="android:layout_height">38dp

下面是 res/layout/activity_main.xml 文件的內容:

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

下面文件 res/values/strings.xml 的內容中定義兩個新的常量:

<string name="app_name">ThemeDemo <string name="action_settings">Settings <string name="hello_world">Hello world! <string name="button_small">Small Font <string name="button_large">Large Font

以下是AndroidManifest.xml文件的默認內容。在這裏,我們不需要改變任何東西,保持主題名稱不變。但是,如果定義了新的主題或繼承默認用不同的名稱,那麼需要明確使用新的名稱,必須要取代AppTheme名稱。

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yiibai.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.yiibai.guidemo.MainActivity" android:label="@string/app_name" > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />

我們嘗試運行ThemeDemo 應用程序,AVD上安裝的應用程序,並啓動它,如果一切設置和應用都沒有問題,它會顯示以下模擬器窗口:

Android主題示例