Android入门:Toast的4种特效

2015-01-24 09:53:25|?次阅读|上传:wustguangh【已有?条评论】发表评论

关键词:Java, Android, 移动应用, 界面设计|来源:唯设编程网

custom.xml对应布局我们在使用自定义Toast的时候用到,下面给出对应的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/llToast"
    android:background="#ffff00">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="提示"
        android:id="@+id/tvTitleToast"
        android:layout_marginLeft="10dp"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/tvTextToast"
        android:layout_alignEnd="@+id/tvTextToast"
        />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvImageToast"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/icon" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="这是一个完全自定义的Toast"
        android:id="@+id/tvTextToast"
        android:layout_below="@+id/tvImageToast" />

</LinearLayout>

icon.png使用导入的一个图片,你可以根据需求选择一张图片即可。

二. Toast用法介绍

下面在MainActivity中添加代码,实现对应按钮的响应,所有功能我们在onCreate函数中完成,所以我们仅仅给出onCreate函数中的代码:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //按钮初始化
        btn_default = (Button) findViewById(R.id.btn_default);
        btn_default.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "默认Toast样式",
                        Toast.LENGTH_SHORT).show();
            }
        });

        btn_def_pos = (Button) findViewById(R.id.btn_def_pos);
        btn_def_pos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(getApplicationContext(),
                        "自定义位置Toast", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        });

        btn_img = (Button) findViewById(R.id.btn_img);
        btn_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(getApplicationContext(),
                        "带图片的Toast", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                LinearLayout toastView = (LinearLayout) toast.getView();
                ImageView imageCodeProject = new ImageView(getApplicationContext());
                imageCodeProject.setImageResource(R.drawable.icon);
                toastView.addView(imageCodeProject, 0);
                toast.show();
            }
        });

        btn_abs_define = (Button) findViewById(R.id.btn_abs_define);
        btn_abs_define.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.custom,
                        (ViewGroup) findViewById(R.id.llToast));
                ImageView image = (ImageView) layout
                        .findViewById(R.id.tvImageToast);
                image.setImageResource(R.drawable.icon);
                TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
                title.setText("提示");
                TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
                text.setText("这是一个完全自定义Toast");
                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER | Gravity.CENTER, 0,0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
            }
        });
    }

需要注意:findViewById需要在setContentView函数之后调用,否则运行APK会出现异常崩溃。

发表评论0条 】
网友评论(共?条评论)..
Android入门:Toast的4种特效