2013-04-17 13 views
6

私はいくつかの情報をユーザに表示したいと思いますが、ユーザが他の場所をタップするか、戻るボタンを押すまで情報を消したくないです。私はこれを明白な選択肢は、(トーストとは対照的に)ダイアログにテキストを表示することであることに気づいています。私はダイアログをシステムトーストに似せて欲しいです。私はただtransient_notification.xmlのレイアウト(およびそれに関連するリソース)をコピーすることができると認識していますが、ToastのスタイルはデバイスとOSによって大きく異なるため、これはうまく一致しません。Android:トースト風のダイアログを作成する良い方法はありますか?

システムトーストのスタイルを継承するダイアログを作成する良い方法はありますか?

+0

トーストがそのように動作することを意図しています。カスタムダイアログを作成し、必要に応じてスタイルを設定することができます。カスタムテーマがあります。問題ではない – Raghunandan

答えて

1

Croutonをカスタマイズして試してみるといいかもしれませんし、触れると解除できます。

1

2つの可能な提案。 1つの可能性は、PopupWindowとカスタムXMLを使用することです。実装するのは難しいことではありません。もう1つの選択肢は、オープンソースプロジェクトのCrouton(https://github.com/keyboardsurfer/Crouton)にあるトースト置換システムを使用することです。基本的には、より美しいUIを提供しています。私はオプションを知っています。デモはPlayストアでダウンロードできます。

3

か、トースト

public static Toast currentToast; 
/** 
* Use a custom display for Toasts. 
* 
* @param message 
*/ 
public static void customToast(String message) { 
    // Avoid creating a queue of toasts 
    if (currentToast != null) { 
     // Dismiss the current showing Toast 
     currentToast.cancel(); 
    }  
    //Retrieve the layout Inflater 
    LayoutInflater inflater = (LayoutInflater) 
      context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    //Assign the custom layout to view 
    View layout = inflater.inflate(R.layout.custom_toast, null); 
    //Return the application context 
    currentToast = new Toast(context.getApplicationContext()); 
    //Set toast gravity to center 
    currentToast.setGravity(Gravity.CENTER | Gravity.CENTER, 0, 0); 
    //Set toast duration 
    currentToast.setDuration(Toast.LENGTH_LONG); 
    //Set the custom layout to Toast 
    currentToast.setView(layout); 
    //Get the TextView for the message of the Toast 
    TextView text = (TextView) layout.findViewById(R.id.text); 
    //Set the custom text for the message of the Toast 
    text.setText(message); 
    //Display toast 
    currentToast.show(); 
    // Check if the layout is visible - just to be sure 
    if (layout != null) { 
     // Touch listener for the layout 
     // This will listen for any touch event on the screen 
     layout.setOnTouchListener(new OnTouchListener() {   
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       // No need to check the event, just dismiss the toast if it is showing 
       if (currentToast != null) { 
        currentToast.cancel(); 
        // we return True if the listener has consumed the event 
        return true; 
       } 
       return false; 
      } 
     }); 
    } 
} 

custom_toast.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="10dip" 
    android:background="@drawable/custom_toast_shape"> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textStyle="bold" 
     android:textColor="@color/blue" 
     android:textSize="16sp" 
     android:text="@string/app_name" 
    /> 

    <View 
     android:layout_gravity="center" 
     android:layout_width="fill_parent" 
     android:layout_height="2dip" 
     android:background="@color/blue" 
    /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/black" 
     android:layout_marginTop="5dp" 
     android:layout_marginBottom="5dp" 
     android:maxEms="15" 
     android:gravity="center_horizontal" 
     android:id="@+id/text" 
    /> 
</LinearLayout> 

Tを表示する

カスタムメソッドカスタムレイアウトを使用することができますOこれはちょうど私が方法を少し変更した

Utils.customToast("Just a test to see if toast is working!\nPerfect"); 

EDIT を呼び出します。今度は、トーストの列を作成せず、通常のトーストのように、タッチで解除されます。

誰がそれを向上させることができた場合は、それを行うこと自由に感じなさい:)

関連する問題