Toast用于向用户显示一些帮助/提示,我在用其他软件时,Toast样式是他们自定义的样式,我也研究了下,做两个小例子,供大家参考,样式见附件。
有不足之处请指点
ToastActivity.java
package com.action;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ToastActivity extends Activity {
private Button btn1;
private Button btn2;
private Toast toast;
private View secondLayout;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
secondLayout = getLayoutInflater().inflate(R.layout.second, null);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new BtnListener(btn1));
btn2.setOnClickListener(new BtnListener(btn2));
}
class BtnListener implements OnClickListener {
private View view;
public BtnListener(View view) {
this.view = view;
}
@Override
public void onClick(View v) {
if (view.equals(btn1)) {
toast = Toast.makeText(getApplicationContext(), "自定义Toast位置",
Toast.LENGTH_LONG); //自定义Toast位置
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
} else if (view.equals(btn2)) {
toast = new Toast(getApplicationContext()); //自定义Toast样式
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setView(secondLayout);
toast.show();
}
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/btn1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button" />
<Button android:id="@+id/btn2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button" />
</LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:background="@drawable/toast_frame"
android:gravity="center_vertical"
android:layout_height="fill_parent">
<ImageView android:id="@+id/img" android:layout_width="wrap_content"
android:src="@drawable/btn_check_buttonless_on" android:layout_height="wrap_content" />
<TextView android:id="@+id/tv" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="second" />
</LinearLayout>
- 大小: 14.8 KB
分享到:
相关推荐
自定义Toast,设置Toast显示位置,自定义Toast的复杂布局 博客地址:http://blog.csdn.net/xy_nyle/article/details/20137953
一个简单的自定义Toast资源,您可以根据自己的需要更改我的代码即可实现您想要的效果(主要是更改xml文件的布局),这是androidstudio的项目,但是不妨碍在eclipse中使用,可以直接在eclipse中新建android项目,然后将...
和对话框不同的是,Toast并不是以独占方式显示的,它并不会抢夺用户的焦点,在弹出Toast的时候,依然可以对之前的界面进行操作,我们在“”一文中介绍了纯文本的Toast的使用,我们完全可以自定义Toast的显示内容和...
主要为大家详细介绍了android自定义Toast设定显示时间,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
自定义Toast,类似Android的Toast
1.自定义Toast提示,高仿IOS的屏幕中间提示,效果可以参考android版腾讯新闻的提示。还有一个重要特点:多次连续点击不会产生多个Toast队列不停的一直显示,而是直接更新当前Toast的视图,效果绝对棒! 2.自定义加载...
Android中自定义Toast.pdf 学习资料 复习资料 教学资源
微信小程序自定义toast的源码,简单详细 具体步骤参考:http://blog.csdn.net/xxs18326183038/article/details/79281651
自定义toast 上下中带图片 加改造的日历选择控件
android 自定义toast,toast自定义显示时间,toast自定义显示样式
一个自定义Toast样式的demo,可以设置Toast的显示位置和显示内容
自定义Toast小部件是Flutter应用程序中非常实用的功能,它可以用于向用户提供各种通知和信息。在本文中,我们学习了如何创建一个简单的自定义Toast小部件,并讨论了如何添加一些进阶功能以满足不同的需求。希望这篇...
android Toast对象的使用 自定义Toast
自定义Toast,防止层叠显示问题,和自定义Toast样式 博客地址:https://blog.csdn.net/qq569699973
默认Toast(大家都会),自定义Toast位置,自定义Toast样式
包括普通Toast,自定义的图片Toast,自定义位置的Toast
自定义Toast及解决重复出现Toast的问题