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

Android-Toast的属性设置

2016-04-24 23:00 411 查看
目标效果:











第一个图为所有的Button控件,从上到下点击分别显示不同的Toast效果。

1.activity_main.xml页面定义Button控件。

activity_main.xml页面:

<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=".MainActivity" >

<Button
android:id="@+id/btnChange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="114dp"
android:text="改变位置Toast" />

<Button
android:id="@+id/btnNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnImage"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="自定义Toast" />

<Button
android:id="@+id/btnToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="默认Toast" />

<Button
android:id="@+id/btnImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnToast"
android:layout_below="@+id/btnChange"
android:layout_marginTop="15dp"
android:text="图片Toast" />

</RelativeLayout>


2.layout文件夹中定义toast.xml页面,作为最后一自定义Toast的布局页面。
toast.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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="自定义1" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="自定义2" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>

</LinearLayout>


3.MainActivity.java页面处理点击事件。
MainActivity.java页面:
package com.example.toast;

import java.util.zip.Inflater;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
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 implements OnClickListener{

private Button btnToast,btnChange,btnImage,btnNew;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnToast=(Button) findViewById(R.id.btnToast);
btnChange=(Button) findViewById(R.id.btnChange);
btnImage=(Button) findViewById(R.id.btnImage);
btnNew=(Button) findViewById(R.id.btnNew);

btnToast.setOnClickListener(this);
btnChange.setOnClickListener(this);
btnImage.setOnClickListener(this);
btnNew.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnToast:
Toast toast1=Toast.makeText(this,"这是一个默认Toast",Toast.LENGTH_SHORT);
toast1.show();
break;
case R.id.btnChange:
Toast toast2=Toast.makeText(this,"这是一个改变位置的Toast",Toast.LENGTH_SHORT);
toast2.setGravity(Gravity.CENTER,0,0);//第二个参数为X的偏移量:正值往右偏移,负值往左偏移;第三个参数为Y的偏移量:正值往下偏移,负值往上偏移
toast2.show();
break;
case R.id.btnImage:
Toast toast3=Toast.makeText(this,"这是一个图片Toast",Toast.LENGTH_SHORT);
LinearLayout toast_layout=(LinearLayout) toast3.getView();//将Toast作为一个布局
ImageView image=new ImageView(this);
image.setImageResource(R.drawable.ic_launcher);   //设置图片
toast_layout.addView(image,0);    //将图片添加到Toast上,0代表位置
toast3.show();
break;
case R.id.btnNew:
LayoutInflater inflater=LayoutInflater.from(this);
View toast_view=inflater.inflate(R.layout.toast, null);
Toast toast4=Toast.makeText(this,"这是一个默认Toast",Toast.LENGTH_SHORT);
toast4.setView(toast_view);       //将布局添加到Toast上
toast4.show();
break;
default:
break;
}
}

}


4.运行就可以显示目标效果了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: