安卓修改系统Toast 字体大小

 * Make a standard toast that just contains a text view.
 *

 * @param context  The context to use.  Usually your {@link android.app.Application}
 *                 or {@link android.app.Activity} object.
 * @param text     The text to show.  Can be formatted text.

 *                 {@link #LENGTH_LONG}
 *

 */

public static Toast makeText(Context context, CharSequence text, 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;
}

我们在Toast的源码里面可以看到在makeText方法里面 通过渲染R.layout.transient_notification这个布局来加载系统默认Toast,所以我们同样通过
getView方法获取布局再找到对应的TextView,就可以修改TextView的属性。下面是修改系统Toast的代码

Toast t = Toast.makeText(mActivity, R.string.setting_softupdate, Toast.LENGTH_LONG);
TextView tv = (TextView) t.getView().findViewById(com.android.internal.R.id.message);
 tv.setTextSize(23);
 t.show();

猜你喜欢

转载自blog.csdn.net/lyjSmile/article/details/80416195
今日推荐