自定义Toast的显示效果
2015-11-19 23:43
337 查看
Activity:
package com.example.editortoast; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.bt).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { toastShow(); } }); } private void toastShow() { LayoutInflater inflater = LayoutInflater.from(getApplicationContext()); View view = inflater.inflate(R.layout.item_toast, null); TextView textView1 = (TextView) view.findViewById(R.id.TextView_1); textView1.setText("Toast1"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(0); toast.setView(view); toast.show(); } }
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" tools:context="com.example.editortoast.MainActivity" > <Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击" /> </RelativeLayout>
item_toast.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="80dp" android:layout_height="80dp" android:layout_centerVertical="true" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/TextView_1" android:textSize="30sp" android:textColor="@android:color/holo_red_light" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/image" /> </RelativeLayout>
相关文章推荐
- 【C语言】【面试题】【笔试题】写一个递归函数DigitSum(n),输入一个非负整数,返回组成它的数字之和
- hibernate3.jar has no source attachment
- 南昌大学软院大神养成计划----网页标签的使用
- 轻轻松松带你了解线程与进程
- 南大软院大神养成计划--第四天单/复选框与文字对齐问题
- OC的内存管理机制以及野指针问题
- C# out ref 重载
- Android照片墙应用实现,再多的图片也不怕崩溃
- HDU 5564:Clarke and digits 收获颇多的矩阵快速幂 + 前缀和
- Python导出Excel为Lua/Json/Xml实例教程(三):终极需求
- HDU 5564:Clarke and digits 收获颇多的矩阵快速幂 + 前缀和
- html5中datalist简单用法
- strstr查找子字符串函数
- 【C语言】【面试题】【笔试题】编写一个函数实现n^k,使用递归实现
- UINavigationController
- ios之performSelector 那点事
- POJ 2001 Shortest Prefixes
- phonegap android 开发环境搭建
- sourcetree的使用
- Linux——磁盘及文件系统