Android开发实现自定义Toast、LayoutInflater使用其他布局示例

发布时间:2019-08-08 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android开发实现自定义Toast、LayoutInflater使用其他布局示例脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了AndROId开发实现自定义Toast、LayoutInflater使用其他布局。分享给大家供大家参考,具体如下:

内容:

1.自定义样式toast

2.再活动中添加其他布局

实现效果:

Android开发实现自定义Toast、LayoutInflater使用其他布局示例

步骤:

一、自定义View 引用zidingyiXMl文件 生成一个布局对象

二、采用Toast 的addView() 方法将该对象添加到Toast对象中

三、显示:Toast.show()

具体实现方法:

 public class MainActivITy extends Activity {   Toast toast;   @override   PRotected void onCreate(Bundle savedInstancestate) {     suPEr.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     //应用布局文件     View insideView = LayoutInflater.From(MainActivity.this).inflate(R.layout.cell, null);     LinearLayout linearLayout = (LinearLayout) insideView.findViewById(R.id.cell);     ImageView imageView = (ImageView) insideView.findViewById(R.id.image1_Toast);     TextView textView = (TextView) insideView.findViewById(R.id.textToast);     imageView.setImageResource(R.drawable.warming);     textView.setText("你的app 炸了!!");     //建立提示消息对象     toast = new Toast(this);     toast.setView(insideView);   }   //按钮点击时弹出   public void prev(View source){     toast.show();   } }  

注:R.layout.cell 中的cell 就是自定义的布局文件

建立步骤 在/values文件夹下 呢哇一个xML文件即可,内容如下:

 <&#63;xml version="1.0" encoding="utf-8"?> <LinearLayout   android:id="@+id/cell"   xmlns:android="http://schemas.android.COM/apk/res/android"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:orientation="horizontal">   <ImageView     android:id="@+id/image1_Toast"     android:layout_width="wrap_content"     android:layout_height="wrap_content"/>   <TextView     android:id="@+id/textToast"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:textSize="15dp"/> </LinearLayout>  

最后给出整体的布局文件

 <?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:gravity="center_horizontal">     <Button       android:onClick="prev"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:layout_alignParentBottom="true"       android:layout_alignParentLeft="true"/> </RelativeLayout>  

注:采用了 android:onClick="prev" 方法 在布局文件中直接添加了点击事件,故MainActivity中不用手动添加onClickListener

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android数据库操作技巧总结》及《Android资源操作技巧汇总

android教程
脚本网站
android studio

脚本宝典总结

以上是脚本宝典为你收集整理的Android开发实现自定义Toast、LayoutInflater使用其他布局示例全部内容,希望文章能够帮你解决Android开发实现自定义Toast、LayoutInflater使用其他布局示例所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。