2011-12-31 5 views
5

私は画像とテキストを持つカスタムトースト通知を持っています。カスタムトーストはうまく動作しますが、私は自分のカスタムトーストをデフォルトトーストのルックアンドフィールを継承させるためにどのようにしたらいいですか?私はそれが良い丸い角と境界線を持つデフォルトのもののように見えるようにします。Android:カスタムトースト通知デフォルトトーストを継承

これは私のカスタムトーストの外観です。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/toast_layout_root" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp" 
    android:background="#DAAA"> 
    <ImageView android:id="@+id/chatIcon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
     android:src="@drawable/ic_chat"/> 
    <TextView android:id="@+id/text" 
     android:text="@string/unread_message_toast" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:textColor="#FFF" 
       /> 
</LinearLayout> 

答えて

6

からこの

<style name="Themename" parent="android:Theme.dialog"> 

を試してみてください。いくつかのことを周りに変更し、それもあなたのために動作するはずです。

Toast ImageToast = new Toast(getBaseContext()); 
       LinearLayout toastLayout = new LinearLayout(
         getBaseContext()); 
       toastLayout.setOrientation(LinearLayout.HORIZONTAL); 
       ImageView image = new ImageView(getBaseContext()); 
       image.setImageResource(R.drawable.easter_egg); 
       toastLayout.addView(image); 
       ImageToast.setView(toastLayout); 
       ImageToast.setDuration(Toast.LENGTH_SHORT); 
       ImageToast.show(); 
+0

素晴らしいことです!非常に柔軟です。これは、レイアウトXMLを追加したり要素のIDを管理したりせずに再利用できます。以前はインフレータの方法を使っていましたが、再利用はそれほど簡単ではありませんでした。 +1 – iozee

+1

これはもう動作しないようです。例外をスローします:java.lang.RuntimeException:このToastはToast.makeText()で作成されていません – Silmarilos

0

は、私は私のアプリケーションの一つで、これを使用developer.android.com/guide/topics/ui/themes.html

0

以下のコードを試してください。

XMLレイアウトからビューを膨張させ、inflated_xml_viewという名前を付けます。

Toast toastView = new Toast(this); 
    toastView.setView(inflated_xml_view); 
    toastView.setDuration(Toast.LENGTH_LONG); 
    toastView.setGravity(Gravity.CENTER, 0,0); 
    toastView.show(); 
+0

ご回答いただきありがとうございます、ごめんなさい。私は今夜​​これを試し、勝者を授与するでしょう。 – EpicOfChaos

1

(ロリポップ上)素敵な角の丸いデフォルトの背景を取得するために使用:

android:background="@android:drawable/toast_frame" 

または

android:background="?android:attr/toastFrameBackground" 

それは、Androidのバージョンに応じて、トーストの背景を与えます最新のものをお探しの場合は、toast_frame.9.pngファイルを..sdk \ platforms \ android- [late STバージョン] \データ\ RES \ drawable- [密度]

テキストスタイル:

android:textAppearance="@android:style/TextAppearance.Toast" 
    android:textColor="@android:color/bright_foreground_dark" 
    android:shadowColor="#BB000000" 
    android:shadowRadius="2.75" 

出典:https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/transient_notification.xml