使用给定的边距方法制作自定义吐司] >>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();
}
}