如何在Android

时间:2016-05-17 09:30:44

标签: java android

我需要在android中显示自定义吐司。这将显示在一个特定的位置。如何在屏幕的特定位置显示自定义吐司。

5 个答案:

答案 0 :(得分:0)

enter image description here您可以使用此库

SuperToasts Library

SuperToasts in github

答案 1 :(得分:0)

来自android developer guide:

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

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

首先获取您的应用程序/活动上下文,定义消息字符串和持续时间,创建一个新的Toast对象并将其传递给上下文,文本消息和持续时间,调用show()。

您可以使用在toast.show()之前调用的setGravity函数来定位toast:

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

答案 2 :(得分:0)

尝试使用此方法

load data local infile 'C:/Users/adam/Downloads/vasarlok.txt'
into table table_name
fields terminated by ',' ignore 1 lines 
(@col1,@col2,@col3,@col4)
set 
table.column_name = @col1,
table.column_name = @col2,
table.column_name = @col3,
table.column_name = @col4;

和dialog_bottom_toast.xml是

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();

}

答案 3 :(得分:0)

实现自定义Toast的非常简单的方法

//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();
    }

将其布局定义为
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"); 

答案 4 :(得分:0)

1。使用TextView创建布局custom_toast.xml,如下面的

    <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。插入此代码以在将消息作为参数的方法中显示toast。 myShowToastMethod(String message){// code}

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();