libro
www.tuyano.com
Google androidプログラミング入門

AppWidgetを作ろう! (3/10)

作成:2009-12-28 17:26
更新:2010-05-11 15:13

■AppWidgetのレイアウトを作る

では、実際に簡単なAppWidgetを作ってみましょう。ここでは「MyAppWidget」という簡単なAppWidgetを作ってみます。ボタンとテキスト表示エリアがあり、クリックすると何かを表示する、というようなシンプルなものを考えてみましょう。

まずは、AppWidgetのレイアウトからです。プロジェクトの「res」内にある「layout」の中に、新たに「appwidget.xml」というファイルを作りましょう。そして以下のリストのようにソースコードを記述しましょう。

TextViewやButtonに表示するテキストは、面倒くさいのでstrings.xmlを使わず直接設定しておきました。TextViewとButtonがあるだけのシンプルなものですね。表示されるテキストからなんとなく想像がつくように、今回作るのはシンプルな運勢占いプログラムです。ボタンを押すと、今日の運勢が表示される、というようなものになります。(といっても、ただランダムにテキストを選んで表示するだけ、ですが)

※プログラムリストが表示されない場合

AddBlockなどの広告ブロックツールがONになっていると、プログラムリスト等が表示されない場合があります。これらのツールをOFFにしてみてください。

●プログラム・リスト●

<?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:background="#33000000"
    android:orientation="vertical">
    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:lines="3"
        android:textColor="#FFFFFF"
        android:text="今日の運勢"
        />
    <Button
        android:id="@+id/button1"
        android:text="占う"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        />
</LinearLayout>
※関連コンテンツ

「Google androidプログラミング入門」に戻る