ウェブアプリケーション,インジェクション,コマンドインジェクション

Androidアプリ開発 Viewをつかったインターフェースデザイン

Viewの部品 ボタンを使ってみる

Using Image Buttons

ボタンviewを使うで示したのは、
アクションの中のButtonとToggleButton、そして、
それらのイベントを処理するいろんな方法でした。
ここでは、ボタンのイメージをどのように表示させるかを示します。
ことわざにあるとおり、画像は千の言葉に値します。
そのため、時にはより直感的に、記述的にボタンの上部に
言葉よりも画像を表示したほうが好まれます。
activity_main.xmlファイルを考えてみます。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

   <ImageButton
    &emsp:android:id="@+id/imageButton"
    &emsp:;android:layout_width="wrap_content"
    &emsp:android:layout_height="wrap_content"
    &emsp:android:src="@drawable/ic_launcher"
    &emsp:onClick="onToggle" />

   <Button
     android:id="@+id/imageTextButton1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:drawableTop="@drawable/ic_launcher"
     android:text="Android" />

   <Button
     android:id="@+id/imageTextButton2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:drawableLeft="@drawable/ic_launcher"
     android:text="Android" />

   <Button
     android:id="@+id/imageTextButton3"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:drawableRight="@drawable/ic_launcher"
     android:text="Android" />

   <Button
     android:id="@+id/imageTextButton4"
     android:layout_width="wrap_content"
     androidlayout_height="wrap_content"
     android:drawableBottom="@drawable/ic_launcher"
     text="Android" />
</LinearLayout>

最初のボタンがImageButtonであることに注意します。
ImageButtonの内容でボタンの上部に画像を表示させることができます。
しかし、テキスト(文字)はImageButtonの中に表示できません。
ボタンの中にテキストを表示させるために属性として、
android:drawableTop
android:drawableLeft
android:drawableRight
android:drawableBottomをボタンviewに使っています。
この属性の指定により、画像を上下左右に配置することができます。
以下のコードの様にすべてを合わせて使うこともでき、
テキストを真ん中に画像が上下左右に配置されます。

<Button
   android:id="@+id/imageTextbutton"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:drawableTop="@drawable/ic_launcher"
   android:drawableLeft="@drawable/ic_launcher"
   android:drawableRight="@drawable/ic_launcher"
   android:drawableBottom="@drawable/ic_launcher"
   android:text="Android" />

プログラムに沿った形でも画像ボタンをセットできます。
UIの中に以下の様なButtonがあると想定します。

<Button
   android:id="@+id/imageTextButton6"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="" />

上で作ったボタンに以下の様なコードで”Android"のテキストを
セットし、ボタンの上部に画像を置きます。

//---Button view---
Button btn = (Button) findViewById(R.id.imageTextButton6);
btn.setText("Android");
btn.setCompoundDrawablesWithIntrinsicBounds(
     0,              //left
     R.drawable.ic_launcher,   //top
     0,              //right
     0);              //bottom
  
ホーム
便利堂ロゴ
inserted by FC2 system