Android软件开发之 自定义别样Toast
转于:http://blog.****.net/jackhenry/article/details/7340278
------------------------------------------------------------------------------------
在Android开发过程中,相信大家对Toast都很熟悉,在一定程度上它可以提高用户体验,比如用户登录浏览器时,浏览器肯定会检测当前设备的网络连接状况,如果当前设备的网络连接是断开的,就可以使用Toast告诉用户,网络初始化失败!让用户检测是否打开数据开关或连接Wifi,如图:
但是这种样式的显示有时候不能满足我们的需求,而且看起来也比较不美观。
然后本人就想重写写个不一样的Toast,再查看了Android的源码后,发现非常简单。
Android源码Toast.java部分代码:
- public static Toast makeText(Context context, CharSequence text, 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;
- }
-----------------------------------------^-^......直接上代码
新的Toast的layout文件 newtoast.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:gravity="center_vertical"
- android:background="@drawable/btn_bg"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView
- android:id="@+id/image0"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- />
- <TextView
- android:id="@+id/text0"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:textSize="10sp"
- android:textColor="#000"
- />
- </LinearLayout>
新Toast背景 btn_bg.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <shape
- xmlns:android="http://schemas.android.com/apk/res/android">
- <corners
- android:radius="6dp"
- />
- <padding
- android:left="5dp"
- android:top="5dp"
- android:right="5dp"
- android:bottom="5dp"
- />
- <solid
- android:color="#FFFFFF"
- />
- </shape>
NewToast.java:
- public class NewToast extends Toast {
- public NewToast(Context context) {
- super(context);
- }
- public static Toast makeText(Context context, int resId, CharSequence text, int duration) {
- Toast result = new Toast(context);
- //获取LayoutInflater对象
- LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- //由layout文件创建一个View对象
- View layout = inflater.inflate(R.layout.newtoast, null);
- //实例化ImageView和TextView对象
- ImageView imageView = (ImageView) layout.findViewById(R.id.image0);
- TextView textView = (TextView) layout.findViewById(R.id.text0);
- imageView.setImageResource(resId);
- textView.setText(text);
- result.setView(layout);
- result.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
- result.setDuration(duration);
- return result;
- }
- }
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"
- >
- <Button
- android:id="@+id/btn0"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/ic_btn_name"
- android:background="@drawable/btn_bg"
- />
- </LinearLayout>
strings.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">NewToastDemo</string>
- <string name="ic_btn_name">测试按钮</string>
- </resources>
最后来测试一下,MainActivity.java:
- public class MainActivity extends Activity implements OnClickListener {
- //声明一个Button对象
- private Button btn;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btn = (Button) findViewById(R.id.btn0);
- btn.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- NewToast.makeText(MainActivity.this, R.drawable.prettygirl, "这是一个自定义的Toast!", Toast.LENGTH_SHORT).show();
- }
- }
run app之后,点击按钮,出现如下图所示:
这样一个新的Toast就写好了。