我正在尝试编写自己的Toast
类实现。
toast_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:background="@android:drawable/toast_frame"
android:layout_gravity="center">
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="This is a toast!" />
</LinearLayout>
然后我尝试淡出和淡出它:
LayoutInflater inflater = this.getLayoutInflater();
View toast = inflater.inflate(R.layout.toast_layout,
(ViewGroup) this.findViewById(R.id.toast_layout_root));
long fade_duration = 1000; // milliseconds
AlphaAnimation fadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
fadeInAnimation.setDuration(fade_duration);
toast.startAnimation(fadeInAnimation);
AlphaAnimation fadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
fadeOutAnimation.setDuration(fade_duration);
fadeOutAnimation.setStartOffset(fade_duration + Toast.LENGTH_LONG);
toast.setAnimation(fadeOutAnimation);
但是什么都没有出现。我做错了什么?
感谢您的回复。
答案 0 :(得分:0)
我个人用于淡化视图的工具方法
public static void fadeIn(View view) {
fadeIn(view, 500);
}
public static void fadeIn(View view, long millis) {
view.animate().alpha(1).setDuration(millis).start();
}
public static void fadeOut(View view) {
fadeOut(view, 500);
}
public static void fadeOut(View view, long millis) {
view.animate().alpha(0).setDuration(millis).start();
}
您可能还想在此之后将其设置为GONE:
public static void fadeOut(final View view, long duration, boolean hideAfter) {
ViewPropertyAnimator animator = view.animate().alpha(0).setDuration(duration);
if (hideAfter) {
animator.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.GONE);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
animator.start();
}
(如果你这样做,请记得下次淡入时将其设置为VISIBLE)
答案 1 :(得分:0)
Toast customtoast=new Toast(context);
customtoast.setView(toast);-->yourtoast
customtoast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,0, 0);
customtoast.setDuration(Toast.LENGTH_LONG);
customtoast.show();
在下面的代码之后添加上面的代码块。
View toast = inflater.inflate(R.layout.toast_layout,
(ViewGroup) this.findViewById(R.id.toast_layout_root));
我希望这对你有所帮助。请参阅参考资料。 http://www.androidinterview.com/android-toast-creating-a-android-custom-toast-example/