Android-theme-demo-example
提供:Dev Guides
Android-テーマのデモの例
次の例は、アプリケーションにテーマを使用する方法を示しています。 デモのために、デフォルトの AppTheme を変更し、デフォルトのテキスト、サイズ、ファミリ、シャドウなどを変更します。 次の手順に従って、シンプルなAndroidアプリケーションの作成から始めましょう-
Step | Description |
---|---|
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. |
以下は、変更されたメインアクティビティファイル 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>
以下は、2つの新しい定数を定義する 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" >
<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からアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[image:/android/eclipse_run.jpg[Eclipse Run Icon]アイコンの実行]をクリックします。 EclipseはあなたのAVDにアプリをインストールし、それを起動し、セットアップとアプリケーションですべてがうまくいけば、次のエミュレータウィンドウが表示されます-