开发者

android toast doesn't fit text

I am developing an application where I have to use numerous toasts.

I display these toasts by using:

Toast.makeText(context, "Some medium-sized text", Toast.LENGTH_SHORT).show();

The displayer toast, however, has the height of one line, while the text is displayed on开发者_运维问答 multiple lines. The result is that I can't view all the text in the toast.

How can I fix this?


Try inserting a carriage-return and line-feed where you want to split the text.

These characters refer back to the old typewriter models. Carriage return was the cylinder moving back to the start and line feed was the cylinder rolling (feeding) by one line.

In computing these are represented by two escaped characters (special codes that allow non-printable codes inside a string by prefixing them with a backslash \).

  • A carriage return is represented by \r
  • A line-feed is represented by \n (you can remember this as a new line).

Some non-unix systems (e.g. Windows) require both, others (e.g Linux on which Android is based) only need the new line but it is generally safe to do both everywhere. The one thing that is essential is the order they are in. It must be \r\n

To put this into your example:

Toast.makeText(context, "First line of text\r\nSecond line of text", Toast.LENGTH_SHORT).show();

In Android you should be able to reduce this to just the new line character \n as unix based systems are not so fussy:

Toast.makeText(context, "First line of text\nSecond line of text", Toast.LENGTH_SHORT).show();


Using the main idea of this Custom toast in android : a simple example and this Android's Toast default colors and alpha I developed a simple custom Toast that looks like the default one but it wraps the text in multilines.

I create a simple class with the makeText(context,text,duration) static method, so I had only to replace Toast.makeText with CustomToast.makeText everywhere in my projects.

Below the code

CustomToast.java

public class CustomToast extends Toast{
    /**
     * 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 CustomToast(Context context) {
        super(context);
    }

    public static Toast makeText(Context context, CharSequence text, int duration) {
        Toast t = Toast.makeText(context,text,duration);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View layout = inflater.inflate(R.layout.custom_toast,null);

        TextView textView = (TextView) layout.findViewById(R.id.text);
        textView.setText(text);

        t.setView(layout);


        return t;
    }

}

The layout layout/custom_toast.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/custom_toast_layout_id"
              android:background="@android:drawable/toast_frame"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="horizontal"
              android:gravity="center_horizontal|center_vertical"
              android:padding="5dp" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_height="wrap_content"
        android:singleLine="false"
        android:layout_weight="1"
        android:textAppearance="@android:style/TextAppearance.Small"
        android:textColor="@android:color/background_light"
        android:shadowColor="#BB000000"
        android:shadowRadius="2.75"/>

</LinearLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜