忍者ブログ

とある社内SEの備忘録 !!

主にRPA、Androidアプリ開発について投稿しています。メモ書きです。

独自LayoutのToastを表示させる

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

独自LayoutのToastを表示させる

画面の背景を白にして、カスタムトーストを表示
LinearLayout ll = (LinearLayout)findViewById(R.id.contentAll);
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.parseColor("#FFFFFF"));
ll.setForeground(gd);
customToast();
カスタムトーストは以下。
private void customToast(){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout, null);

// 画像を設定
ImageView toastImage = (ImageView) layout.findViewById(R.id.toastImage);
toastImage.setImageResource(R.drawable.toast);

// テキストを設定
TextView toastText = (TextView) layout.findViewById(R.id.toastText);
toastText.setText("今日も1日頑張ろう\nご安全に!");

Toast toast = new Toast(this);
toast.setView(layout);
// 中央に表示
toast.setGravity( Gravity.CENTER, 0, 0 );
toast.show();
}
トーストのレイアウト
toast_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:background="@drawable/button_red"
>
<TextView android:id="@+id/toastText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#333"
android:text="本日もご安全に!\n今夜は飲みに行こうか。"
android:layout_gravity="center"
android:gravity="center_vertical"
android:textSize="48px"
/>
<ImageView
android:layout_gravity="center"
android:id="@+id/toastImage"
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_marginRight="10dip"
android:src="@drawable/muroi"
/>
</LinearLayout>
PR

コメント

プロフィール

HN:
Satoshin
性別:
男性
職業:
開拓エンジニア
趣味:
ダーツ、麻雀、カラオケ、ジョギング
自己紹介:
横浜在住の30代♂

某企業の情報システム部門に所属。
⇒転職活動中

業務効率化を推進しつつ自社用Androidアプリを開発している。
言語:Java、VB.NET、VBA、ASP.NET、C#
興味:Kotlin、Python、AWS、UiPath

横浜圏の友人皆無・・(´;ω;`)
RPA、ダーツ、麻雀、飲み仲間募集中w

麻雀格闘倶楽部はココに出没中

AbemaTVでM.LEAGUE鑑賞中!!

P R