Toast消息文本开箱即用

问题描述 投票:5回答:1

我在后执行时从异步任务显示吐司,但如下图所示显示异常。您能帮我解决这个问题吗?

enter image description here

android android-asynctask toast android-toast
1个回答
2
投票

使用给定的边距方法制作自定义吐司] >>

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View rowView = inflater.inflate(R.layout.toast, null);
            TextView text = (TextView) rowView.findViewById(R.id.text);
            text.setText("Application Not Available");
            setMargins(text,10,5,10,5);
            Toast toast = new Toast(context.getApplicationContext());
            toast.setGravity(Gravity.BOTTOM, 0, 85);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(rowView);
            toast.show();

通话后方法

setMargins(yourView, 0, 4, 0, 8);

并将保证金值放入此方法中

private void setMargins(View view, int left, int top, int right, int bottom){
  if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams){ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams)view.getLayoutParams();final float scale = context.getResources().getDisplayMetrics().density;
        // convert the DP into pixel
        int l = (int) (left * scale + 0.5f);
        int r = (int) (right * scale + 0.5f);
        int t = (int) (top * scale + 0.5f);
        int b = (int) (bottom * scale + 0.5f);
        p.setMargins(l, t, r, b);
        view.requestLayout();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.