2013-06-04 75 views
21

我是Android新手,正在開發一個示例應用程序。我想了解我們如何定製默認的Android Toast。 我想改變吐司的顏色,風格和其他屬性。如何在Android中自定義Toast

我們可以在Toast中添加Image嗎?

我讀計算器

How to Customise Toast in Android?下面的帖子。 customize toast in android

但這些都不能解釋如何在Toast中添加圖像。

+1

一旦與Toast相處融洽,並且知道它的缺點,我相信你會發現[** THIS **](https://github.com/keyboardsurfer/Crouton)有用! – Leeeeeeelo

回答

33

是的,我們可以更改吐司的顏色,大小,位置和其他屬性。 我們還可以將圖像添加到吐司。

一個好的博客這個How To Customize Toast In Android所有內容從這個博客

你可以創建一個XML和它充氣吐司拍攝。

你也可以做它在運行時

LinearLayout layout=new LinearLayout(this); 
    layout.setBackgroundResource(R.color.LightOrange); 

    TextView tv=new TextView(this); 
    // set the TextView properties like color, size etc 
    tv.setTextColor(Color.RED); 
    tv.setTextSize(15);   

    tv.setGravity(Gravity.CENTER_VERTICAL); 

    // set the text you want to show in Toast 
    tv.setText("My Custom Toast at Bottom of Screen"); 

    ImageView img=new ImageView(this); 

    // give the drawble resource for the ImageView 
    img.setImageResource(R.drawable.myimage); 

    // add both the Views TextView and ImageView in layout 
    layout.addView(img); 
    layout.addView(tv); 

    Toast toast=new Toast(this); //context is object of Context write "this" if you are an Activity 
    // Set The layout as Toast View 
    toast.setView(layout); 

     // Position you toast here toast position is 50 dp from bottom you can give any integral value 
    toast.setGravity(Gravity.BOTTOM, 0, 50); 
    toast.show(); 
10
LayoutInflater inflater = (LayoutInflater) 
          activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View layout = inflater.inflate(R.layout.cred_menu_like_popup, (ViewGroup) 
           activity.findViewById(R.id.like_popup_layout)); 
ImageView imageView = (ImageView) layout.findViewById(R.id.like_popup_iv); 

TextView text = (TextView) layout.findViewById(R.id.like_popup_tv); 
text.setText("Like"); 

Toast toast = new Toast(activity.getApplicationContext()); 
toast.setGravity(Gravity.BOTTOM, 0, 200); 
toast.setDuration(Toast.LENGTH_LONG); 
toast.setView(layout); 

toast.show(); 

這裏是佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/like_popup_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@drawable/customshapetransparent" 
    android:paddingTop="35dp" 
    android:paddingBottom="25dp" 
    android:paddingLeft="35dp" 
    android:paddingRight="35dp" 
    > 
    <ImageView 
     android:id="@+id/like_popup_iv" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:layout_centerHorizontal="true" 
     /> 
    <TextView 
     android:id="@+id/like_popup_tv" 
     android:layout_below="@id/like_popup_iv" 
     android:layout_marginTop="10dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:textStyle="bold" 
     android:textColor="@android:color/white" 
     android:textSize="20sp" 
     /> 
</RelativeLayout> 

自定義形狀佈局是:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" >  
    <solid android:color="#60000000" /> 
    <corners android:radius="8dp" /> 
</shape> 
3

要自烤麪包的形狀和顏色使用這個。

Toast toast = Toast.makeText(getApplicationContext(), "You not Subscribe Try again", Toast.LENGTH_LONG); 
          View vieew = toast.getView(); 
          // vieew.setBackgroundColor(Color.parseColor("#BD8BDC")); 
          vieew.setBackgroundResource(R.drawable.textinputborder); 
          toast.setView(vieew); 
toast.show(); //This displays the toast for the specified lenght. 

在R.drawable.textinputborder還可以使用

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle"> 
      <solid android:color="#83BB66" /> 
      <stroke android:width="1dp" 
        android:color="#1B200A" /> 
      <corners android:radius="20dp" /> 
     </shape> 
    </item> 
</selector> 
1

首先設計您的自定義界面...爲了簡單起見,我設計如下定製UI:

<?xml version="1.0" encoding="utf-8"?> 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:id="@+id/CustomToastLayoutRoot" 
 
    android:orientation="vertical"> 
 

 
    <ImageView 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:src="@drawable/close" 
 
     android:id="@+id/imageView" /> 
 

 
    <TextView 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:text="Warning !!!" 
 
     android:id="@+id/textView" 
 
     android:layout_gravity="bottom" /> 
 
</LinearLayout>

Toast toast = new Toast(MainActivity.this); 
      toast.setGravity(Gravity.CENTER, 0, 0); 
      toast.setDuration(Toast.LENGTH_LONG); 

      LayoutInflater li = getLayoutInflater(); 
      View toastAppear = li.inflate(R.layout.customtoast_layout, (ViewGroup) findViewById(R.id.CustomToastLayoutRoot)); 
      toast.setView(toastAppear); 
      toast.show(); 
相關問題