2013-06-04 7 views
21

Ich bin neu in Android und arbeitet an einem Beispiel-App. Ich möchte erfahren, wie wir den Standard Android Toast anpassen können. Ich mag die Farbe, Stil und andere Attribute von Toast ändern.Wie Anpassen Toast In Android

Können wir Bild in Toast als auch hinzufügen?

las ich folgenden Beitrag auf Stackoverflow

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

aber keiner von ihnen erklärt, wie Bild in Toast hinzuzufügen.

+1

Sobald Sie zusammen (gut) mit Toast bekommen, und seine Nachteile weiß, ich bin sicher, Sie finden [** THIS **] (https://github.com/keyboardsurfer/Crouton) nützlich! – Leeeeeeelo

Antwort

33

Ja können wir die Farbe, Größe, Position und andere Attribute von Toast ändern. Wir können Toast auch ein Bild hinzufügen.

Ein guter Blog für diesesHow To Customize Toast In Android Alle Inhalte aus diesem Blog

aufgenommen werden, können Sie eine XML erstellen und Toast aufzublasen.

Sie auch, es kann zur Laufzeit zu tun

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

Hier ist das 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> 

Benutzerdefinierte Form Layout ist:

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

Um Toast Form anpassen und Farbe verwende das.

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. 

in R.drawable.textinputborder verwenden Auch

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

Erster Entwurf Ihre individuelle Schnittstelle ... Einfachheit halber i wie unten angepasst UI Design:

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