在《Toast使用1》中介绍了Toast的简单使用, 在这一节中,我们深入一点。
Toast显示位置的设置
默认的Toast显示在底部的水平中间,向下偏移64dip。我们可以通过二种方式,设置Toast的显示位置:
1. 通过setGravity(int
gravity, int xOffset, int yOffset) 来设置显示的位置。
Toast toast = Toast.makeText(this,"设置Toast的显示位置在屏幕中央",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
2. 通过setGravity(int gravity, int xOffset, int yOffset) 来设置显示的位置。
关于这个方法的说明,看下面官方的说明:
Set the margins of the view.(设置视图的栏外空白)
Parameters
horizontalMargin |
The horizontal margin, in percentage of the container width, between the container's edges and the notification 容器的边缘与提示信息的横向空白(与容器宽度的比) |
---|---|
verticalMargin |
The vertical margin, in percentage of the container height, between the container's edges and the notification 容器的边缘与提示信息的纵向空白(与容器高度的比)。 |
改变Toast的显示内容
在一个应用中,我们可能多次使用Toast,我们不能在每次使用的时候都创建Toast,但是情景不同Toast显示的信息也不一样,如果是通过 Toast.makeText()方法得到的Toast实例,那么你可以放心大胆的使用下面的方法要满足这种需求:
toast.setText("改变内容");
如果是自定义的View,那么需要慎重。从源代码角度
我们使用Toast.makeText()的方法很方便的创建了一个Toast,那么这个Toast是如何被创建的呢?我们可以看看这个方法的源代码:
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);
//mNextView就是Toast显示样式的View
result.mNextView = v;
result.mDuration = duration;
return result;
}
所以我们猜测,实际上,这个方法也是加载了一个transient_notification的布局,而这个布局的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/res/layout/transient_notification.xml
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<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>
因此我们可以自定义Toast的样式。Toast是不能获取焦点的,单单是展示我们想要展示的消息用的,因此不要在Toast中设置监听事件等滑稽的事情,和对话框不一样。
我们先创建一个Toast的布局:layout_toast.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <ImageView android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="自定义的消息" android:id="@android:id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
然后设置Toast的view
View view = getLayoutInflater().inflate(R.layout.layout_toast,null);
toast.setView(view);
toast.show();
这样自定义的样式就出来了。
我们可能多次使用Toast,但不是每次使用Toast就创建这个对象,而是通过改变Toast的显示的信息就可以了,要满足这个需求,使用Toast的setText方法就可以了,那么对于自定义的Toast,如果做到相同的效果呢,其实蛮简单的,在定义自己的样式的时候,如果TextView显示的是相关的信息,那么设置TextViewid为:
android:id="@android:id/message"
那么就能够顺利的使用该方法了。
这是这个方法的源代码。
public void setText(CharSequence s) {
if (mNextView == null) {
throw new RuntimeException("This Toast was not created with Toast.makeText()");
}
TextView tv = (TextView) mNextView.findViewById(com.android.internal.R.id.message);
if (tv == null) {
throw new RuntimeException("This Toast was not created with Toast.makeText()");
}
tv.setText(s);
}