demo16Toast

/Users/alamps/AndroidStudioProjects/demo16Toast/demo16Toast/src/main/res/layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">



    <Button android:id="@+id/_shortShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Short Toast" />

    <Button android:id="@+id/_longShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Long Toast" />

    <Button android:id="@+id/_customShow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="customShow Toast" />


</LinearLayout>
================
/Users/alamps/AndroidStudioProjects/demo16Toast/demo16Toast/src/main/java/com/example/demo16toast/MainActivity.java

package com.example.demo16toast;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Button shortShow;
    private Button longShow;
    private Button customShow;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.shortShow = (Button)super.findViewById(R.id._shortShow);
        this.longShow = (Button)super.findViewById(R.id._longShow);
        this.customShow = (Button)super.findViewById(R.id._customShow);

        this.shortShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"Short Toast show ",Toast.LENGTH_SHORT).show();

            }
        });

        this.longShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"Long Toast Show ",Toast.LENGTH_LONG).show();
            }
        });

        this.customShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             Toast customToast= Toast.makeText(MainActivity.this,"Custom Toast show ",Toast.LENGTH_LONG);

            customToast.setGravity(Gravity.CENTER,60,30);

            LinearLayout customToastView =(LinearLayout)customToast.getView();
            ImageView customImageView= new ImageView(MainActivity.this);
            customImageView.setImageResource(R.drawable.ic_launcher);
            customToastView.addView(customImageView,0);//在文字上方填加图片View
            customToast.show();
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

 

posted @ 2016-03-13 18:38  Alamps  阅读(212)  评论(2编辑  收藏  举报