首页 > 代码库 > 自己定义Toast
自己定义Toast
.指定Toast通知的位置
自己定义Toast:
怎样自己定义Toast??
第一步:
写一个XML样式自己定义Toast显示样式
在res/layout下创建一个xml文件toast(文件名称自己定义)
在MainActivity中
自己定义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="http://www.mamicode.com/@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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。