您的位置:首页 > 移动开发 > Android开发

Android中Toast的使用

2015-11-23 22:24 477 查看

Android Toast的使用

1.默认用法

Toast.makeText(getApplicationContext(),
"默认Toast",
Toast.LENGTH_SHORT).show();


getAppliationContext()可以获取到当前App下任意的位置所有的Context,既全局有效

getContext表示获取当前对象所在的context

this表示当前类是context的子类



2.带图片的Toast

toast = Toast.makeText(getApplicationContext(),
"带图片的自定义Toast",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView iv = new ImageView(getApplicationContext());
iv.setImageResource(R.mipmap.ic_launcher);
toastView.addView(iv, 0);
toast.show();


toastView.addView() //0表示文本在下方,1表示文本在上方

toast.setGravity(Gravity.BOTTOM, 0, 0); //设置显示位置

在Toast的Layout中加入ImageView显示图片



3.完全自定义的Toast

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toastcustom, (ViewGroup) findViewById(R.id.layoutCustomL1));

ImageView imageView = (ImageView) layout.findViewById(R.id.ivCustomImage);
imageView.setImageResource(R.mipmap.ic_launcher);

TextView title = (TextView) layout.findViewById(R.id.tvCustomTitle);
title.setText("这是标题");
TextView text = (TextView) layout.findViewById(R.id.tvCUstomText);
text.setText("这是自定义的内容");

toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.BOTTOM,0,0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();


inflater用来找res/layout/下的XML文件,并且实例化,

findViewById()是找XML文件下的具体的widget控件(如Button,TextView)

对于没有被载入或者想要动态载入的界面需要用LayoutInflater.inflate()载入

对于已经载入的界面,可以用findViewById()来获得其中的界面元素

获得LayoutInflater实例的方式

- LayoutInflater inflater = getLayoutInflater();//调用Activity的getLayoutInflater()
- LayoutInflater inflater = LayoutInflater.from(context);
- LayoutInflater inflater =  (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);


自定义Toast思路:

-定义一个Toast Layout布局

-在布局中加入imageView和TextView

-载入imageView和TextView

-设置imageView的属性

-设置TextView的属性

-设置Toast属性



代码及布局文件

1.代码文件

package com.forcoding.omg;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Tos extends AppCompatActivity implements View.OnClickListener {
private Button btnDefaultToast;
private Button btnCustomLocationToast;
private Button btnCustomPictureToast;
private Button btnAllCustomToast;
private Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tos);
findViewById(R.id.btnDefaultToast).setOnClickListener(this);
findViewById(R.id.btnCustomLocationToast).setOnClickListener(this);
findViewById(R.id.btnCustomPictureToast).setOnClickListener(this);
findViewById(R.id.btnAllCutsomToast).setOnClickListener(this);
}

public void onClick(View v) {
Toast toast = null;
switch (v.getId()){
case R.id.btnDefaultToast:
toast.makeText(getApplicationContext(), "默认Toast", Toast.LENGTH_SHORT).show();
break;
case R.id.btnCustomLocationToast:
toast = Toast.makeText(getApplicationContext(), "自定义的Toast",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,100,0);//竖直方向和水平方向偏移
toast.show();
break;
case R.id.btnCustomPictureToast:
toast = Toast.makeText(getApplicationContext(),"带图片的自定义Toast",Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView iv = new ImageView(getApplicationContext());
iv.setImageResource(R.mipmap.ic_launcher);
toastView.addView(iv, 0);//0表示文本在下方,1表示文本在上方
toast.show();

break;
case R.id.btnAllCutsomToast:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toastcustom, (ViewGroup) findViewById(R.id.layoutCustomL1));

ImageView imageView = (ImageView) layout.findViewById(R.id.ivCustomImage);
imageView.setImageResource(R.mipmap.ic_launcher);

TextView title = (TextView) layout.findViewById(R.id.tvCustomTitle);
title.setText("这是标题");
TextView text = (TextView) layout.findViewById(R.id.tvCUstomText);
text.setText("这是自定义的内容");

toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.BOTTOM,0,0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();

break;

}

}

}


2.布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_toast"
tools:context="com.example.forev.helloworld.Toasts">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="默认"
android:id="@+id/btnDefaultToast"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义显示位置"
android:id="@+id/btnCustomLocationToast"
android:layout_below="@+id/btnDefaultToast"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义带图片"
android:id="@+id/btnCustomPictureToast"
android:layout_below="@+id/btnCustomLocationToast"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="完全自定义"
android:id="@+id/btnAllCutsomToast"
android:layout_below="@+id/btnCustomPictureToast"
android:layout_alignStart="@+id/btnCustomPictureToast" />
</RelativeLayout>


3.自定义Toast布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layoutCustomL1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvCustomTitle"
android:layout_gravity="center"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:id="@+id/layoutCustomL2">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ivCustomImage"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvCUstomText"
android:layout_gravity="center"
/>
</LinearLayout>
</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: