android自定义Toast
来源:互联网 发布:python黑帽子和灰帽子 编辑:程序博客网 时间:2024/06/06 18:55
Toast是Android中的一个常用组件,用法很简单,主要用于提示信息的显示且 不能获得焦点。
不能改变他的事件但是我们可以改变他的样式。
首先我们来看一下平时是怎么使用toast的: Toast.makeText(this,"sehngmsadf",Toast.LENGTH_LONG).show();
那么为什么这样就可以显示一个toast呢?让我们看一下andorid的源码是如何实现的
public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
可以看到makeText里面首先是载入了一个layout:com.android.internal.R.layout.transient_notification
然后findviewbyid得到一个textview 将我们的文字信息设置上去
再来看一下这个布局的具体内容
<?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"
android:background="?android:attr/toastFrameBackground">
<TextView
android:id="@android:id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:textAppearance="@style/TextAppearance.Toast"
android:textColor="@color/bright_foreground_dark"
android:shadowColor="#BB000000"
android:shadowRadius="2.75"
/>
</LinearLayout>
可以看到这只是一个简单的linearlayout布局,布局里面有一个ID= message的textview组件而已。
所以我们可以仿照这个布局自定义一个自己喜欢的,这里我就只是改变了一下文字的大小而已。
toast_self.xml
<?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"
android:background="@drawable/toast_background">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:textColor="@color/bright_foreground_dark"
android:textSize="20dp"
android:shadowColor="#BB000000"
android:shadowRadius="5.25"
/>
</LinearLayout>
背景
toast_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<corners android:radius="25dp" />
<solid android:color="#bb414040" />
<padding
android:bottom="10dp"
android:top="10dp"
android:left="30dp"
android:right="30dp" />
</shape>
</item>
</layer-list>
- android自定义Toast视图
- Android中自定义Toast
- Android自定义Toast样式
- android Toast 自定义样式
- 自定义Toast - Android代码
- android中自定义toast
- Android 自定义Toast
- android自定义Toast视图
- android 自定义toast
- 自定义Android Toast
- android自定义toast
- Android Toast自定义
- android自定义toast
- Android之自定义Toast
- Android 自定义Toast实现
- android 自定义toast
- Android Toast自定义
- android 自定义toast
- ThinkPHP的静态缓存,非系统写法。
- Android TextView借助Linkify,使用自定义模式设置链接
- Mac下的VI bundle配置
- TSL工作过程
- 解决get方法传递URL参数中文乱码问题
- android自定义Toast
- 项目开发中常用的HTML标签
- [内存管理]智能指针的好帮手weak_ptr
- CDUESTC 2016 假期赛1 C题
- 队列---链式存储
- saveorupdate与merge的区别
- Kylin使用之创建Cube和高级设置
- ubuntu如何实现双屏显示
- Timer实现数字时钟