Customview像Toast [英] Customview like Toast

查看:70
本文介绍了Customview像Toast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个customview.我想在android中像烤面包一样显示customview.

I have a customview. I want to show the customview like a toast in android.

Toast.makeText(getApplicationContext(),"text",Toast.LENGTH_LONG).show()

也就是说,我希望通过更改基本属性(例如文本),将自定义视图显示在应用程序中的任何位置(在应用程序中全局).

That is, i want my custom view to showup anywhere in the app(globally within the app) by changing basic attributes like text.

请指出正确的方向.

请注意:我不想使用addView()和removeView()添加和删除自定义视图.在我的情况下,自定义Toast也不起作用,因为我需要使用此customview.

Please note: I dont want to use addView() and removeView() to add and remove custom view. Customizing Toast will also not work in my case because i need to use this customview.

推荐答案

扩展Toast类非常简单

比方说,我的自定义吐司的口号是NexoolCustomToast,它扩展了Toast类.下面是自定义Toast类的代码.

Let say My Custom Toast calss is NexoolCustomToast which extends Toast class. Below is the code of custom Toast Class.

   import android.app.Activity;
   import android.app.Application;
   import android.content.Context;
   import android.view.Gravity;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.widget.TextView;
   import android.widget.Toast;

   /**
    * Created by Abhishek on 24-03-2017.
    */

   /**
    * By Default shows Error i.e. Fail toast
    * */
   public class NexoolCustomToast extends Toast {

       private Context mContext;

       private View mView;

       private LayoutInflater mLayoutInflater;

       private TextView mTitleTextView, mMessageTextView;

       /**
        * Construct an empty Toast object.  You must call {@link #setView} before you
        * can call {@link #show}.
        *
        * @param context The context to use.  Usually your {@link Application}
        *                or {@link Activity} object.
        */
       public NexoolCustomToast(Context context) {
           super(context);
           mContext = context;
           mLayoutInflater = LayoutInflater.from(context);
           mView = mLayoutInflater.inflate(R.layout.nexool_fail_custom_toast, null);
           initialiseView(mView);
           setView(mView);
           setGravity(Gravity.TOP | Gravity.END, 0, 0);
       }

       public NexoolCustomToast(Context context, boolean state) {
           super(context);
           mContext = context;
           mLayoutInflater = LayoutInflater.from(context);
           if (state) {
               mView = mLayoutInflater.inflate(R.layout.nexool_success_custom_toast, null);
           } else {
               mView = mLayoutInflater.inflate(R.layout.nexool_fail_custom_toast, null);
           }
           initialiseView(mView);
           setView(mView);
           setGravity(Gravity.TOP | Gravity.END, 0, 0);
       }

       private void initialiseView(View mView) {

           mTitleTextView = (TextView) mView.findViewById(R.id.titleTextView);

           mMessageTextView = (TextView) mView.findViewById(R.id.messageTextView);

       }

       public void setTitle(String title) {

           if (title != null && title.length() != 0) {

               mTitleTextView.setText(title);

           } else {

               mTitleTextView.setVisibility(View.GONE);

           }

       }

       public void setMessage(String message) {

           if (message != null && message.length() != 0) {

               mMessageTextView.setText(message);

           } else {

               mMessageTextView.setVisibility(View.GONE);

           }

       }

       @Override
       public void show() {
           super.show();
       }

       @Override
       public void cancel() {
           super.cancel();
       }

       public static NexoolCustomToast makeText(Context mContext, String mTitle, String mMessage) {
           NexoolCustomToast mNexoolCustomToast = new NexoolCustomToast(mContext);

           mNexoolCustomToast.setTitle(mTitle);

           mNexoolCustomToast.setMessage(mMessage);

           return mNexoolCustomToast;
       }

       public static NexoolCustomToast makeText(Context mContext, String mTitle, String mMessage, boolean state) {
           NexoolCustomToast mNexoolCustomToast = new NexoolCustomToast(mContext, state);

           mNexoolCustomToast.setTitle(mTitle);

           mNexoolCustomToast.setMessage(mMessage);

           return mNexoolCustomToast;
       }
   }

在此类中,我使用来自nexool_fail_custom_toast.xml,nexool_success_custom_toast.xml的XML布局,具有四个布局屏幕尺寸(大布局,大布局,正常布局,小布局,大布局).

In this class I am using xml layout from nexool_fail_custom_toast.xml, nexool_success_custom_toast.xml with four layout screen size(layout-large, layout-normal, layout-small, layout-xlarge).

      //layout-large/nexool_fail_custom_toast.xml
      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:background="@android:color/white"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="?android:attr/actionBarSize"
                android:layout_marginBottom="15dp"
                android:layout_marginRight="15dp"
                android:elevation="5dp">

                <ImageButton
                    android:id="@+id/cancelToastImageButton"
                    android:layout_width="25dp"
                    android:layout_height="match_parent"
                    android:background="@android:color/holo_red_dark"
                    android:scaleType="centerInside"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="15dp">

                    <TextView
                        android:id="@+id/titleTextView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Error Title"
                        android:minWidth="300sp"
                        android:textColor="@android:color/holo_red_dark"
                        android:textAppearance="?android:attr/textAppearanceMedium" />

                    <TextView
                        android:id="@+id/messageTextView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Error Message"
                        android:minWidth="300sp"
                        android:paddingTop="10dp"
                        android:textColor="@android:color/black"
                        android:textAppearance="?android:attr/textAppearanceSmall"/>

                </LinearLayout>


            </LinearLayout>

        </RelativeLayout>







       //layout-large/nexool_success_custom_toast.xml
       <?xml version="1.0" encoding="utf-8"?>
       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="@android:color/transparent">

           <LinearLayout
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:orientation="horizontal"
               android:background="@android:color/white"
               android:layout_alignParentEnd="true"
               android:layout_marginTop="?android:attr/actionBarSize"
               android:layout_marginBottom="15dp"
               android:layout_marginRight="15dp"
               android:elevation="5dp">

               <ImageButton
                   android:id="@+id/cancelToastImageButton"
                   android:layout_width="25dp"
                   android:layout_height="match_parent"
                   android:background="@android:color/holo_green_light"
                   android:scaleType="centerInside"/>

               <LinearLayout
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:orientation="vertical"
                   android:padding="15dp">

                   <TextView
                       android:id="@+id/titleTextView"
                       android:layout_width="match_parent"
                       android:layout_height="wrap_content"
                       android:text="Error Title"
                       android:minWidth="300sp"
                       android:textColor="@android:color/holo_green_light"
                       android:textAppearance="?android:attr/textAppearanceMedium" />

                   <TextView
                       android:id="@+id/messageTextView"
                       android:layout_width="match_parent"
                       android:layout_height="wrap_content"
                       android:text="Error Message"
                       android:minWidth="300sp"
                       android:paddingTop="10dp"
                       android:textColor="@android:color/black"
                       android:textAppearance="?android:attr/textAppearanceSmall"/>

               </LinearLayout>


               </LinearLayout>

               </RelativeLayout>

现在通过更改文本大小或这些文件内部视图的宽度和高度,使以上两个xml文件分别用于布局小,布局普通,布局xlarge.

Now make above two xml files for layout-small, layout-normal, layout-xlarge by changing text size or width and height of views which are inside of these files.

**如何使用**

用于绿色成功布局

For green success layout

    NexoolCustomToast.makeText(mContext, "Success", "Success Message", true).show();

用于红色故障布局

For red failure layout

    NexoolCustomToast.makeText(mContext, "Fail", "Fail Message", false).show();

    //OR

    NexoolCustomToast.makeText(mContext, "Fail", "Fail Message").show();

这篇关于Customview像Toast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆