back

AppWidgetを作ろう! (3/10)

■AppWidgetのレイアウトを作る

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

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

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


(by. SYODA-Tuyano.)

※リストが表示されない場合

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>



   

記事のリストに戻る



PC Site G+ mail