当为Toast使用setDuration()时,是否可以设置一个自定义长度或至少比Toast. length_long更长的长度?
当前回答
一个自定义背景和视图的祝酒词对我来说很管用。我在nexus 7平板电脑上测试了它,我注意到在循环过程中没有淡入淡出动画。实现如下:
public static void customToast(Context context, String message, int duration) {
for (int i = 0; i < duration; i++) {
Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.toast_layout, null);
TextView textViewToast = (TextView) view
.findViewById(R.id.textViewToast);
textViewToast.setText(message);
toast.setView(view);
toast.show();
}
}
下面是上面代码中使用的自定义textview:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textViewToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/fragment_background"
android:padding="8dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/blue" />
@drawable/fragment_background正在使我的吐司有圆角,就像kitkat版本一样。您还可以在该文件中添加其他视图。任何改进和评论的修改都是鼓励的,因为我计划在我的live应用程序中实现这一点。
其他回答
简单地使用SuperToast在任何情况下都可以做出优雅的吐司。让你的吐司颜色鲜艳。编辑你的字体颜色和大小。希望这对你来说都是一体的。
LENGTH_SHORT和LENGTH_LONG的值分别为0和1。这意味着它们被视为标志,而不是实际的持续时间,所以我认为不可能将持续时间设置为这些值以外的任何东西。
如果想要长时间地向用户显示消息,可以考虑使用状态栏通知。当状态栏通知不再相关时,可以通过编程方式取消它们。
使用Crouton,这是一个非常灵活的吐司库。
油煎面包块
你可以像烤面包一样使用它:
Crouton.makeText(context, "YOUR_MESSAGE", Style.INFO);
或者你甚至可以更深入一点,自定义更多,比如将时间设置为无限!例如,在这里我想显示一个toast消息,直到用户通过点击它来确认它。
private static void showMessage(final Activity context, MessageType type, String header, String message) {
View v = context.getLayoutInflater().inflate(R.layout.toast_layout, null);
TextView headerTv = (TextView) v.findViewById(R.id.toastHeader);
headerTv.setText(header);
TextView messageTv = (TextView) v.findViewById(R.id.toastMessage);
messageTv.setText(message);
ImageView toastIcon = (ImageView) v.findViewById(R.id.toastIcon);
final Crouton crouton = getCrouton(context, v);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Crouton.hide(crouton);
}
});
crouton.show();
}
private static Crouton getCrouton(final Activity context, View v) {
Crouton crouton = Crouton.make(context, v);
crouton.setConfiguration(new Configuration.Builder().setDuration(Configuration.DURATION_INFINITE).build());
return crouton;
}
自定义布局,将膨胀为吐司。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="@drawable/shadow_container"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="@dimen/default_margin"
tools:ignore="Overdraw">
<ImageView
android:id="@+id/toastIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/default_spacing_full"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/toastHeader"
style="@style/ItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/toastMessage"
style="@style/ItemSubText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
创建略长的消息的一个非常简单的方法如下:
private Toast myToast;
public MyView(Context context) {
myToast = Toast.makeText(getContext(), "", Toast.LENGTH_LONG);
}
private Runnable extendStatusMessageLengthRunnable = new Runnable() {
@Override
public void run() {
//Show the toast for another interval.
myToast.show();
}
};
public void displayMyToast(final String statusMessage, boolean extraLongDuration) {
removeCallbacks(extendStatusMessageLengthRunnable);
myToast.setText(statusMessage);
myToast.show();
if(extraLongDuration) {
postDelayed(extendStatusMessageLengthRunnable, 3000L);
}
}
注意,上面的示例消除了LENGTH_SHORT选项以保持示例简单。
通常不希望使用Toast消息在很长时间间隔内显示消息,因为这不是Toast类的预期目的。但是有时候,你需要显示的文本量可能需要用户超过3.5秒的时间来阅读,在这种情况下,稍微延长时间(例如,如上所示,到6.5秒)可能是有用的,并且与预期的用法一致。
你可能想试试:
for (int i=0; i < 2; i++)
{
Toast.makeText(this, "blah", Toast.LENGTH_LONG).show();
}
使时间加倍。如果你指定3而不是2,它会使时间增加三倍等等。
推荐文章
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器
- Android命令行工具sdkmanager总是显示:警告:无法创建设置