码迷,mamicode.com
首页 > 其他好文 > 详细

自己定义Toast

时间:2017-05-31 21:32:14      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:xtend   data-   sch   ttext   protect   htm   class   main   布局   

.指定Toast通知的位置
自己定义Toast:


怎样自己定义Toast??
第一步:
写一个XML样式自己定义Toast显示样式
在res/layout下创建一个xml文件toast(文件名称自己定义)
<?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:layout_width="match_parent" android:layout_height="wrap_content" android:background="#448899" android:text="你好" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </LinearLayout>




在MainActivity中


package com.example.checkboxtext;


import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;






public class MainActivity extends Activity {






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




	public void toastshow(View view){
		//		Toast toast = Toast.makeText(MainActivity.this, "toastshow", 0);
		//		toast.setText("你好");
		//		toast.setGravity(Gravity.TOP|Gravity.LEFT, 30, 200);//显示在距离顶部200,距离left30的位置
		//		toast.show();


		Toast toast = new Toast(MainActivity.this);
		View view2 = View.inflate(MainActivity.this, R.layout.toast, null);//把一个布局转化成一个view对象
		toast.setView(view2);
		toast.setDuration(0);//定义时间长短0(短)或1(长)
		toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
		toast.show();


	}
}




自己定义Toast

标签:xtend   data-   sch   ttext   protect   htm   class   main   布局   

原文地址:http://www.cnblogs.com/jzdwajue/p/6925871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!