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的子类
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显示图片
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>
相关文章推荐
- android开发技巧——仿新版QQ锁屏下弹窗(转)
- android活动的启动模式
- Android调用“百度一下”接口
- Android第三方开源水面波浪波形view:WaveView(电量、能量、容量指示)
- Android之支付宝设计与开发
- Android-Universal-Image-Loader 的使用
- Android DiskLruCache缓存完全解析
- android中Baseadapter的 getItem 和 getItemId 的作用和重写
- android112 jni 把java的字符串转换成c的字符串,数组处理
- [Android]toolbar的定义和应用
- Android中实现消息推送(JPush)
- android小问题:代码中设置Button被选中
- Android数据库
- Windows系统下如何优化Android Studio
- android 普通蓝牙源码解读
- WebView 中的 JavaScript 调用 Android 的方法
- greenDao---orm框架
- Android自定义控件之仿京东商城下拉刷新
- Android开发:关于在启动页面就会报相机异常等类似问题
- Android之Adapter