欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 自定义吐司通知

程序员文章站 2022-07-04 22:32:08
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) fin... ......
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);

        button = (button) findviewbyid(r.id.button);

        button.setonclicklistener(new onclicklistener() {

            @override
            public void onclick(view v) {
                // todo auto-generated method stub

                toast toast = new toast(mainactivity.this);

                view view = layoutinflater.from(mainactivity.this).inflate(r.layout.toast, null);

                imageview imageview = (imageview) view.findviewbyid(r.id.image);
                imageview.setimageresource(r.drawable.a1);

                // 居中显示
                toast.setgravity(gravity.center, 0, 0);

                // 加载自定义布局
                toast.setview(view);
                toast.show();
            }
        });
    }
<?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:orientation="horizontal" >

    <imageview
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginright="10dp" />

    <textview
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="自定义吐司通知"
        android:textcolor="#000000" />

</linearlayout>