2013-06-04 13 views
21

Sono nuovo su Android e sto lavorando su un'app campione. Voglio imparare come possiamo personalizzare il Toast Android predefinito. Voglio cambiare il colore, lo stile e altri attributi di Toast.Come personalizzare il toast in Android

È possibile aggiungere l'immagine anche in Toast?

leggo seguente post su StackOverflow

How to Customise Toast in Android?. customize toast in android

ma nessuno di questi spiega come aggiungere l'immagine in Toast.

+1

Una volta che vai avanti (bene) con Toast, e conosci i suoi svantaggi, sono sicuro che troverai [** THIS **] (https://github.com/keyboardsurfer/Crouton) utile! – Leeeeeeelo

risposta

33

Sì, è possibile modificare il colore, la dimensione, la posizione e altri attributi di Toast. Possiamo anche aggiungere un'immagine a Toast.

un buon blog per questoHow To Customize Toast In Android Tutti i contenuti sono tratte da questo blog

È possibile creare un XML e gonfiarlo Toast.

Si può anche farlo in fase di esecuzione

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

Ecco il layout

<?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> 

layout forma d'ordinazione è:

<?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

Per personalizzare la forma Toast e il colore usa questo.

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. 

anche l'uso in 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

primo progetto l'interfaccia personalizzata ... per semplicità i design interfaccia utente personalizzata, come di seguito:

<?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(); 
Problemi correlati