このエントリーをはてなブックマークに追加

2010年5月24日月曜日

Android TextView with Image



[In English]
I sometimes have the case to arrange the image next to the characters.
We can do it by putting TextView and ImageView into Layout.
But today I introduce the other way using only TextView.
The following sample code is how to show the image next to text.
(show four image(left, top, right, bottom of text))

final TextView textView = (TextView)findViewById(R.id.diet_log_label);
final Drawable iconDrawable = getResources().getDrawable(R.drawable.icon);
textView.setCompoundDrawablesWithIntrinsicBounds(iconDrawable, iconDrawable, iconDrawable, iconDrawable);
// or
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon);

To show only left image, write "setCompoundDrawablesWithIntrinsicBounds(iconDrawable, null, null, null)".



[In Japanese]
時々文字の隣に画像を配置したい場合があります。
そんな時は、TextViewとImageViewをLayout内に配置することで実現することもできますが、今日はTextViewだけを使った別の方法を紹介したいと思います。
以下は、テキストの上下左右にアイコン画像を表示させるサンプルコードになります。


final TextView textView = (TextView)findViewById(R.id.diet_log_label);
final Drawable iconDrawable = getResources().getDrawable(R.drawable.icon);
textView.setCompoundDrawablesWithIntrinsicBounds(iconDrawable, iconDrawable, iconDrawable, iconDrawable);
// or
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon);

テキストの左だけ画像を表示させる場合は、setCompoundDrawablesWithIntrinsicBounds(iconDrawable, null, null, null)とします。

0 件のコメント:

コメントを投稿