2017-11-30 4 views
1

사용자 정의보기가 있습니다. 안드로이드에서 커스텀 뷰를 축배처럼 보여주고 싶습니다. 이다토스트와 같은 사용자 정의

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

, 난 내 사용자 정의보기 텍스트와 같은 기본 속성을 변경하여 (전 세계적으로 응용 프로그램 내) 응용 프로그램의 아무 곳이나 showup 싶다.

올바른 방향으로 알려주십시오.

참고 : addView() 및 removeView()를 사용하여 사용자 정의보기를 추가하거나 제거하지 않으려합니다. 커스텀 토스트는 커스텀 뷰를 사용해야하기 때문에 제 경우에는 작동하지 않습니다.

+0

당신은', 토스트 토스트 = 새로운 토스트 (Activity.this)를 사용할 수 있습니다; toast.setView ("귀하의보기"); '이렇게. –

+0

토스트의 소스 코드를 읽을 수 있습니다 : https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/java/android/widget/Toast.java – diegoveloper

답변

3

사용자 정의 토스트를 소유 할 수 있습니다. 메소드는 텍스트와 show.have 모양을 취합니다.

public void showToast(String msg) { 

     LayoutInflater li = getLayoutInflater(); 
     View layout = li.inflate(R.layout.custom_toast, 
     (ViewGroup) findViewById(R.id.custom_toast_layout)); 
     TextView toastmsg = (TextView) layout.findViewById(R.id.custom_toast_message); 
     toastmsg.setText(msg); 
     Toast toast = new Toast(this); 
     toast.setDuration(Toast.LENGTH_LONG); 
     toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); 
     toast.setView(layout); 
     toast.show(); 


    } 

custom_toast_message.xml이 같은

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:id="@+id/custom_toast_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/toast_bg" 
     android:gravity="center_vertical" 
     android:orientation="horizontal"> 

     <ImageView 
      android:id="@+id/custom_toast_image" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="Custom Toast" 
      android:padding="15dp" 
      android:src="@drawable/icon_toast_alert" /> 

     <TextView 
      android:id="@+id/custom_toast_message" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="Custom Toast" 
      android:padding="15dp" 
      android:text="Custom Toast" 
      android:textAlignment="center" 
      android:textColor="@color/colorWhite" 
      android:textSize="18sp" /> 
    </LinearLayout> 

</LinearLayout> 

사용자 정의 토스트 모양 :

enter image description here

코딩

해피!

3

그것 정말 쉽게 당신이 토스트 클래스를

하자를 확장하여 그것을 할 수 있습니다 내 사용자 정의 토스트 CALSS는 토스트 클래스를 확장 NexoolCustomToast 말한다. 아래는 주문형 토스트 클래스의 코드입니다. 내가 nexool_fail_custom_toast.xml에서 XML 레이아웃, 네 개의 레이아웃 화면 크기 (레이아웃 큰 레이아웃 - 정상, 레이아웃 작은 레이아웃 - 초대형)와 nexool_success_custom_toast.xml를 사용하고이 클래스에서

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; 
     } 
    } 

.
 //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 파일 위에 만든다.

** 빨간색 실패 레이아웃

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

    //OR 

    NexoolCustomToast.makeText(mContext, "Fail", "Fail Message").show(); 
녹색 성공 레이아웃

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

를 들어 **

를 사용하는 방법

Custom Toast

+0

나는 ondraw를 사용하고 싶습니다. 어쨌든 거기에 있습니다. 나는 그것을 할 수 있었다 ?? – Jim

+0

예, 토스트와 같은 구조가 아닌 CustomView를 만들 때 onDraw를 사용할 수 있습니다. 안드로이드 토스트는 시간 인스턴스에 대한 메시지를 토스트하는 이점을 제공합니다. OnDraw를 사용하려면 로직을 개발해야합니다. – Abhishek

0

Common.java 파일에 공통 메소드를 작성하십시오.

public static void displayCustomToast(Activity activity, String message, String length) { 

    // if you want to set typeface for toast text then use this // 
    Typeface tfShruti = Typeface.createFromAsset(activity.getAssets(), "fonts/shruti.ttf"); 

    LayoutInflater inflater = activity.getLayoutInflater(); 

    View layout = inflater.inflate(R.layout.custom_layout, 
      (ViewGroup) activity.findViewById(R.id.custom_toast_layout_id)); 
    // set a message 
    TextView text = (TextView) layout.findViewById(R.id.tv_toast); 
    text.setText(message); 
    text.setTypeface(tfShruti); 

    // Toast... 
    Toast toast = new Toast(activity.getApplicationContext()); 
    toast.setGravity(Gravity.BOTTOM, 0, 0); 
    //toast.setMargin(0,10); 
    //toast.setGravity(Gravity.TOP | Gravity.LEFT, 40, 60); 
    //toast.setDuration(Toast.LENGTH_LONG); 

    if (length.equalsIgnoreCase("short")) { 
     toast.setDuration(Toast.LENGTH_SHORT); 
    } else { 
     toast.setDuration(Toast.LENGTH_LONG); 
    } 
    toast.setView(layout); 
    toast.show(); 
} 

이와 같은 레이아웃을 만듭니다.

custom_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/custom_toast_layout_id" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="20dp" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    android:background="@drawable/rectangle_fill_black_color" 
    android:orientation="vertical" 
    android:padding="5dp"> 

    <TextView 
     android:id="@+id/tv_toast" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello" 
     android:textColor="#ffffff" 
     android:textSize="17sp" /> 


</LinearLayout> 

는이 같은 프로젝트에 어느 곳이 방법을 사용합니다.

Common.displayCustomToast(activity, message, "long"); 

또는

Common.displayCustomToast(activity, context.getResources().getString(R.string.please_check_internet), "short"); 
관련 문제