1. 程式人生 > >Android中修改toast的顯示位置

Android中修改toast的顯示位置



在xml中設定onclick屬性值,內部寫入一個String資料,在class檔案中使用此String作為方法名,即可實現其跳轉
public class ToastActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast);
    }

    public void
onClickDefaultToast(View v) { Toast.makeText(this, "預設位置", Toast.LENGTH_LONG).show(); } public void onClickCenterToast(View v) { Toast toast = Toast.makeText(this, "居中顯示", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } public
void onClickTopToast(View v) { Display display = getWindowManager().getDefaultDisplay(); int height = display.getHeight(); Toast toast = Toast.makeText(this, "上部", Toast.LENGTH_LONG); toast.setGravity(Gravity.TOP, 0, height / 6); toast.show(); } }
在xml中設定onclick屬性值,內部寫入一個String資料,在class檔案中使用此String作為方法名,即可實現其跳轉
public class ToastActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast);
    }

    public void onClickDefaultToast(View v) {
        Toast.makeText(this, "預設位置", Toast.LENGTH_LONG).show();
    }

    public void onClickCenterToast(View v) {
        Toast toast = Toast.makeText(this, "居中顯示", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public void onClickTopToast(View v) {
        Display display = getWindowManager().getDefaultDisplay();
        int height = display.getHeight();
        Toast toast = Toast.makeText(this, "上部", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0, height / 6);
        toast.show();
    }
}