Toast taToast = Toast.makeText(MainActivity.this, null, Toast.LENGTH_SHORT); taToast.setGravity(Gravity.CENTER, 180, 0);中心向右180px taToast.setText("我是自定义土司"); taToast.setDuration(Toast.LENGTH_SHORT); taToast.show();
Toast taToast = new Toast(MainActivity.this); taToast.setDuration(Toast.LENGTH_SHORT); taToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0); LinearLayout llLayout = new LinearLayout(MainActivity.this); ImageView view = new ImageView(MainActivity.this); view.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher)); llLayout.addView(view); TextView tv = new TextView(MainActivity.this); tv.setText("我是。。"); tv.setBackgroundDrawable(getResources().getDrawable(R.drawable.green)); llLayout.addView(tv); taToast.setView(llLayout); taToast.show();
第二种:
Toast toast = new Toast(MainActivity.this); toast.setGravity(Gravity.CENTER, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); // LayoutInflater inflater = getLayoutInflater(); // View view = inflater.inflate(R.layout.toast, null); toast.setView(View.inflate(MainActivity.this, R.layout.toast,null)); toast.show();