2013-07-23 62 views
6

デフォルトのトーストを変更してカスタムレイアウトを作成せずに自分のトーストをカスタマイズしたいと思います。私はトーストの背景のために赤い色を、トーストのテキストの色を白い色にして、トーストの背景をそのデフォルトトーストより大きくしたい。私のアプリケーションを実行すると、私のトーストから何も変わっていない、それはまだデフォルトトーストで表示されます。アンドロイドでトーストの背景色、背景色、テキスト色をカスタマイズする方法

これは私が私のトーストをカスタマイズする方法である:

if (seriesSelection == null) { 
    Toast toast = Toast.makeText(getApplicationContext(), "tidak ada chart yang dipilih", Toast.LENGTH_SHORT); 
    toast.setGravity(Gravity.CENTER, 50, 50); 
    toast.getView().setPadding(10, 10, 10, 10); 
    toast.getView().setBackgroundColor(Color.RED); 
    TextView text = (TextView) toast.getView().findViewById(android.R.id.message); 
    text.setTextColor(Color.WHITE); 
    text.setTextSize(14); 
} else { 
    Toast toast= Toast.makeText(
      getApplicationContext(), 
      "Nilai " + listData.get(seriesSelection.getPointIndex()).getInuNilai()+ 
      " tanggal " + listData.get(seriesSelection.getPointIndex()).getTanggal(), 
      Toast.LENGTH_SHORT); 
    toast.setGravity(Gravity.CENTER, 50, 50); 
    toast.getView().setPadding(10, 10, 10, 10); 
    toast.getView().setBackgroundColor(Color.RED); 
    text.setTextColor(Color.WHITE); 
    text.setTextSize(14); 
    toast.show(); 
} 
+0

私の場合、「else」のケースでは、赤いトーストで白いテキストに適用されたパディングが表示されます。 – sandrstar

答えて

9

カスタムビューでカスタムビューを展開し、toast.setView(layout)を使用できます。

例:それはトーストを示した場合、他のコードの一部(別途)

http://developer.android.com/guide/topics/ui/notifiers/toasts.html

@

LayoutInflater inflater = getLayoutInflater(); 
View layout = inflater.inflate(R.layout.custom_toast, 
           (ViewGroup) findViewById(R.id.toast_layout_root)); 

TextView text = (TextView) layout.findViewById(R.id.text); 
text.setText("This is a custom toast"); 

Toast toast = new Toast(getApplicationContext()); 
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); 
toast.setDuration(Toast.LENGTH_LONG); 
toast.setView(layout); 
toast.show(); 

そして、あなたのxml

<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="8dp" 
       android:background="#DAAA" 
       > 
    <ImageView android:src="@drawable/droid" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="8dp" 
       /> 
    <TextView android:id="@+id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textColor="#FFF" 
       /> 
</LinearLayout> 

詳細情報は、あなたの蘭赤い背景と白いテキストの色です。私は何の問題も見ません。しかし、カスタマイズする必要がある場合は、カスタムレイアウトを使用してレイアウトを膨張させ、ビューをトーストに設定することができます。

編集:

あなたのTextView

TextView text = (TextView) toast.getView().findViewById(android.R.id.message); 

は部分とそれ以外の部分のTextViewには初期化されていない場合には初期化されます。

ifおよびelseコードの外側でtextviewを初期化します。

チェックあなたは便利な

https://github.com/keyboardsurfer/Crouton

+0

デフォルトトーストを変更してカスタムレイアウトを作成せずに自分のトーストをカスタマイズしたいと思います。私はできますか? –

+1

@AoyamaNanamiあなたは上記の投稿をチェックすることができます。あなたのコードもうまく動作し、赤い背景と白いテキストのトーストを見ることができます。だから正確な問題は何か。私はあなたの問題を再現できません。私はあなたのコードをコピー貼り付けて実行しました。それはうまく動作します。カスタムレイアウトを使用すると、カスタマイズのオプションが増えます。 – Raghunandan

+0

ありがとうございます。 –

2

トーストsetView()方法があります。

任意のビューを表示するようにトーストをカスタマイズできます。

私はトースト内のビューを編集するのではなく、ビューを作成して自分でポップすると言っています。

+0

あなたはどのようにそれをカスタムする方法を伝えることができますか? –

0

私はそれに応じてトーストをカスタマイズするための非常にシンプルで簡単なコードを持っているかもしれませんクルトンと呼ばれるこのライブラリは、あなたもトーストとテキストの色の背景を変更することができます。

Toast toast = Toast.makeText(MainActivity.this, "Added successfully", Toast.LENGTH_LONG); 
    View view1 = toast.getView(); 
    toast.getView().setPadding(20, 20, 20, 20); 
    view1.setBackgroundResource(R.color.GREEN); 
    view1.setTextColor(Color.RED); 
    toast.show();