Toast

作者: acc8226 | 来源:发表于2017-02-22 19:37 被阅读17次

    http://developer.android.youdaxue.com/guide/topics/ui/notifiers/toasts.html#Positioning

    创建自定义Toast View

    To create a custom layout, define a View layout, in XML or in your application code, and pass the root View object to the setView(View) method.
    For example, you can create the layout for the toast visible in the screenshot to the right with the following XML (saved as layout/custom_toast.xml):

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/custom_toast_container"
                  android:orientation="horizontal"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:padding="8dp"
                  android:background="#DAAA"
                  >
        <ImageView android:src="@drawable/droid"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_marginRight="8dp"
                   />
        <TextView android:id="@+id/text"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textColor="#FFF"
                  />
    </LinearLayout>
    

    Notice that the ID of the LinearLayout element is "custom_toast_container". You must use this ID and the ID of the XML layout file "custom_toast" to inflate the layout, as shown here:

    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.custom_toast,
                    (ViewGroup) findViewById(R.id.custom_toast_container));
    
    TextView text = (TextView) layout.findViewById(R.id.text);
    text.setText("This is a custom toast");
    
    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();
    

    相关文章

      网友评论

          本文标题:Toast

          本文链接:https://www.haomeiwen.com/subject/xcgmwttx.html