Android_Toast

xml文件:

main1:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.toastdemo.MainActivity" >

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       android:gravity="center"
       >
       <Button
           android:id="@+id/btn_1"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="显示带有图片的toast"
           android:textSize="20sp"/>
       <Button
           android:id="@+id/btn_2"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="自定义的Toast"
           android:textSize="20sp"/>

   </LinearLayout>

</RelativeLayout>

main2.xml

<?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="vertical" >
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定义内容"/>
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>

</LinearLayout>

源代码:

package com.example.toastdemo;

import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    private Button btn_1;
    private Button btn_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_1 = (Button) findViewById(R.id.btn_1);
        btn_2 = (Button) findViewById(R.id.btn_2);

        /**
         * 显示带有图片的Toast
         */
        btn_1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast toast = Toast.makeText(MainActivity.this, "show  Toast with the picture", Toast.LENGTH_SHORT);
                LinearLayout toastLayout = (LinearLayout) toast.getView();
                ImageView image = new ImageView(MainActivity.this);
                image.setImageResource(R.drawable.ic_launcher);
                toastLayout.addView(image,0);//设置图片于文字上
                //设置toast显示的位置,其中X方向偏移量左-右+,y方向的偏移量上-下+
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        });
        /**
         * 显示自定义的Toast
         */
        btn_2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast toast = new Toast(MainActivity.this);
                LayoutInflater layout_inflate = LayoutInflater.from(MainActivity.this);
                View inflater = layout_inflate.inflate(R.layout.main2, null);
                toast.setView(inflater);
                toast.show();
            }
        });
    }
}
时间: 2024-10-09 11:58:00

Android_Toast的相关文章

Android_Toast使用

Toast类是位于android.widget包下面的一个类. 其中 有两个常量 LENGTH_SHORT = 0和LENGTH_LONG = 1;分别代表短时间提醒和长时间提醒. 一般我们使用Toast不需要自己创建Toast对象,而是使用它的 makeText(Context context,CharSequence text,int duration) 静态方法,这个方法内部会使用context值来调用构造函数自动创建一个Toast对象.新建一个使用text设定的TextView,并配合c

android 开发-Toast控件的实现

Toast吐司: Toast内容简单,不做过多介绍,Toast支持自带简单吐司,自定义吐司.内容简单可见代码,详见API.A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. Fo