showToast多行居中顯示

makeText源碼:

    public static Toast makeText(Context context, CharSequence text, @Duration int duration) {  
            Toast result = new Toast(context);  

            LayoutInflater inflate = (LayoutInflater)  
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
            View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);  
            TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);  
            tv.setText(text);  

            result.mNextView = v;  
            result.mDuration = duration;  

            return result;  
        }  

其佈局文件:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:orientation="vertical"  
        android:background="?android:attr/toastFrameBackground">  

        <TextView  
            android:id="@android:id/message"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_weight="1"  
            android:layout_gravity="center_horizontal"  
            android:textAppearance="@style/TextAppearance.Toast"  
            android:textColor="@color/bright_foreground_dark"  
            android:shadowColor="#BB000000"  
            android:shadowRadius="2.75"  
            />  

    </LinearLayout>  

自定義Toast居中顯示:
toast.getView().findViewById(textview_id)).setGravity(Gravity.CENTER);

public static void show(Context context, String str) {  
        if (toast == null)  
            toast = Toast.makeText(context.getApplicationContext(), str, Toast.LENGTH_SHORT);  
        else  
            toast.setText(str);  
        if (textview_id == 0)  
            textview_id = Resources.getSystem().getIdentifier("message", "id", "android");  
        ((TextView) toast.getView().findViewById(textview_id)).setGravity(Gravity.CENTER);  
        toast.show();  
    }  

    public static void show(Context context, int resId) {  
        if (toast == null)  
            toast = Toast.makeText(context.getApplicationContext(), resId, Toast.LENGTH_SHORT);  
        else  
            toast.setText(resId);  
        if (textview_id == 0)  
            textview_id = Resources.getSystem().getIdentifier("message", "id", "android");  
        ((TextView) toast.getView().findViewById(textview_id)).setGravity(Gravity.CENTER);  
        toast.show();  
    }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章