2013-07-23 394 views
6

我想通過修改默認Toast來自定義我的烤麪包而不創建自定義佈局。我想要烤麪包的背景爲紅色,烤麪包的文字顏色爲白色,我想讓烤麪包的背景更大,以表示默認吐司。當我運行我的應用程序時,我的敬酒沒有任何變化,它仍然顯示默認烤麪包。如何自定義android中Toast的背景,背景顏色和文本顏色

這是我如何自定義我舉杯:

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

對我來說,'其他'案例顯示紅色烤麪包與白色文本與應用填充。 – sandrstar

回答

9

你可以有一個自定義視圖充氣自定義視圖,並使用toast.setView(layout)

例子:

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> 

更多信息@

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

然你如果和代碼的其他部分(分別)它顯示敬酒與紅色背景和白色文本顏色。我沒有看到任何問題。但是,如果您需要自定義您可以使用自定義佈局並擴大布局並將視圖設置爲敬酒。

編輯:

你的TextView

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

在if部分和其他部分的TextView未初始化初始化。

在if和else代碼外初始化textview。

檢查這個庫稱爲crouton,你可能會發現有用

https://github.com/keyboardsurfer/Crouton

+0

我想通過修改默認Toast來自定義我的烤麪包而不創建自定義佈局。我可以嗎? –

+1

@AoyamaNanami你可以查看上面的帖子。你的代碼也可以正常工作,我可以看到紅色背景和白色文本。那麼確切的問題是什麼。我無法重現您的問題。我通過複製粘貼來運行你的代碼。它工作正常。使用自定義佈局會爲您提供更多自定義選項。 – Raghunandan

+0

它的工作..謝謝:) –

2

吐司有setView()方法。

您可以自定義Toast以顯示任何視圖。

我想說,不是試圖編輯Toast內部的視圖,而是創建一個視圖並將它自己彈出。

+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();