2016-05-17 3 views
-1

アンドロイドにカスタマイズトーストを表示する必要があります。特定の位置に表示されます。どのように画面の特定の位置にカスタムトーストを表示できますか。Androidのアクティビティでカスタムトーストを特定の位置に表示する方法

+0

の提案として、あなたはトーストが様々な位置に表示させたい場合は、このhttp://stackoverflow.com/a/37668673/6312749 – Abhishek

答えて

0

1としてそれを呼び出します下のようなテキストビュー

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="800dp" 
    android:layout_height="wrap_content" 
    android:background="@drawable/custom_tost_shape" 
    android:gravity="center_vertical|center_horizontal" 
    android:text="Action Successfuly Completed!" 
    android:textColor="#ff0000" 
    android:textSize="40sp"/> 

2.このコードを挿入して、メッセージをパラメータとして受け取るメソッドにトーストを表示します。 myShowToastMethod(文字列のメッセージ){//コード}

LayoutInflater layoutInflater = GenApplicationId.this.getLayoutInflater(); 
      View view = layoutInflater.inflate(R.layout.custom_toast, null); 
      TextView tvMessage = (TextView) view.findViewById(R.id.textView1); 
      tvMessage.setHeight(100); 
      tvMessage.setText(message); 
      Toast toast = new Toast(getApplicationContext()); 
      toast.setDuration(Toast.LENGTH_LONG); 
      toast.setGravity(Gravity.TOP, -0, 230); 
      toast.setView(view); 
      toast.show(); 
+0

を参照することができ、私はあなたが好きです回答。どうもありがとうございました。 –

0

enter image description hereあなたは、あなたのアプリケーション/活動のコンテキストを取得し、このライブラリ

SuperToastsライブラリ

android developer guide:

Context context = getApplicationContext(); 
CharSequence text = "Hello toast!"; 
int duration = Toast.LENGTH_SHORT; 

Toast toast = Toast.makeText(context, text, duration); 
toast.show(); 

当初からSuperToasts in github

0

を使用するメッセージ文字列と期間を定義することができ、新しいToastオブジェクトを作成し、コンテキスト、テキストメッセージ、およびduratiに渡しますon、show()を呼び出します。

あなたが前(toast.showするために呼び出さsetGravity機能)を使用してトーストを配置することができます

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0); 
0

が、この方法に

public static void showBottomToast(Context context, String msg) { 
    View custom_view = LayoutInflater.from(context).inflate(
      R.layout.dialog_bottom_toast, null); 

    TextView tvMessage = (TextView) custom_view 
      .findViewById(R.id.tvMessage); 
    tvMessage.setText(msg); 

    showToast(context.getApplicationContext(), Gravity.FILL_HORIZONTAL 
      | Gravity.BOTTOM, custom_view, Toast.LENGTH_LONG); 
} 

public static void showToast(Context ctx, int gravity, View root, 
     int duration) { 
    final Toast mToast = new Toast(ctx); 
    mToast.setGravity(gravity, 0, 0); 
    mToast.setDuration(duration); 
    mToast.setView(root); 
    mToast.show(); 

} 

とdialog_bottom_toast.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/tvMessage" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:gravity="center_horizontal" 
    android:layout_margin="@dimen/marg_20" 
    android:text="@string/welcome" 
    android:maxLines="2" 
    android:ellipsize="end" 
    android:textColor="@color/blue" 
    android:textSize="@dimen/sp_16" 
    android:textStyle="bold" /> 

</LinearLayout> 
あるを使用してみてください
0

非常に簡単カスタムトーストを実装する方法

//show custom Toast in android 
    private void showCustomToast(String showToast) { 
     LayoutInflater inflater = getLayoutInflater(); 
     View layout = inflater.inflate(R.layout.toast_layout, 
       (ViewGroup) findViewById(R.id.toast_layout_root)); 
     TextView text = (TextView) layout.findViewById(R.id.text); 
     text.setText(showToast); 
     Toast toast = new Toast(getApplicationContext()); 
     toast.setDuration(Toast.LENGTH_LONG); 
     toast.setView(layout); 
     toast.show(); 
    } 

とレイアウトcustom_toast.xmlを作成することが
toast_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/toast_layout_root" 
    android:layout_width="fill_parent" 
    android:layout_height="40dp" 
    android:background="@color/primary" 
    android:orientation="horizontal" 
    android:padding="10dp"> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:text="hello" 
     android:textSize="15sp" 
     android:textColor="#ffffff" /> 
</LinearLayout> 

ようなレイアウトだ定義し、これを呼び出したいところはどこでもそしてちょうど

showCustomToast("Your_Message"); 
関連する問題