Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Android自定义Toast

Android系统自带的Toast可能不是那么给力,有时候我们需要自定义,先来一个效果图步骤如下:1 定义Toast的样式 toast.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hahahahhahah"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/black"
    android:padding="6dp" >    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/app_name"
            android:src="@drawable/android" >
        </ImageView>        <TextView
            android:id="@+id/toastTextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingTop="4dp"
            android:text="这是标题栏"
            android:textColor="@color/white"
            android:textIsSelectable="true"
            android:textSize="20sp" />
    </LinearLayout>    <TextView
        android:id="@+id/toastTextView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:paddingBottom="4dp"
        android:text="这是要显示的内容"
        android:textColor="@color/white"
        android:textIsSelectable="true"
        android:textSize="12sp" /></LinearLayout>2 自定义Toast,引用我们自己的布局 View contentView = (LinearLayout) LayoutInflater
    .from(this).inflate(R.layout.toast, null);
  Toast toast=new Toast(this);
  toast.setGravity(Gravity.CENTER,0,0);
  toast.setView(contentView);
  toast.setDuration(Toast.LENGTH_LONG);
  toast.show();这样就显示出刚才看到的效果了,大家可以在加入监听器等其他的内容更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11