下面的例子演示如何使用的應(yīng)用程序的主題(Theme)。修改默認(rèn) AppTheme 的默認(rèn)文本,改變它的大小,系列,陰影等?,F(xiàn)在開(kāi)始創(chuàng)建一個(gè)簡(jiǎn)單的Android應(yīng)用程序按照以下步驟:
| 步驟 | 描述 |
|---|---|
| 1 | 使用Android Studio創(chuàng)建一個(gè)Android應(yīng)用程序,并將其命名為:ThemeDemo |
| 2 | 修改 src/MainActivity.java 文件,以添加click事件偵聽(tīng)器和處理程序定義的兩個(gè)按鈕 |
| 3 | 定義樣式風(fēng)格在全局樣式文件 res/values/style.xml 定義應(yīng)用程序裝飾按鈕的文本,改變默認(rèn)主題自定義屬性 |
| 4 | 修改 res/layout/activity_main.xml 文件的默認(rèn)內(nèi)容包括一套Android的UI控件,并使用已定義樣式 |
| 5 | 定義 res/values/strings.xml 文件所需的常量 |
| 6 | 運(yùn)行該應(yīng)用程序啟動(dòng) Android模擬器并驗(yàn)證應(yīng)用程序所做的修改結(jié)果 |
以下是主活動(dòng)文件 src/com.yiibai.guidemo9/MainActivity.java 修改后的內(nèi)容,這個(gè)文件可以包括每個(gè)生命周期的基本方法。
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 定義的內(nèi)容:
<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內(nèi)容提供者下一篇:Android本地化