为什么SetGravity onToast没有效果?

时间:2015-07-21 20:57:43

标签: android

我有一个尝试创建自定义Toast对象的活动。我希望吐司出现在屏幕的中央,但不管我设定的重力是什么,吐司都会出现在最顶层。看起来我给吐司的设置对发生的事情没有影响。我该怎么做才能将吐司移到中心。

创建吐司的代码:

{
"html": {
"abbreviations": {
    "perchafter":"<perch:after>",
    "perchbefore":"<perch:before>"
    },

  "snippets": {
    "perchcontent": "<?php perch_content('|'); ?>"
    "perchevery": "<perch:every count="|"></perch:every>"
}


  }
}

布局膨胀的xml:

LayoutInflater inflater = (LayoutInflater) context.getSystemService
                            (Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.notification_layout,null);

((TextView) layout.findViewById(R.id.title)).setText(title);
((TextView) layout.findViewById(R.id.content)).setText(content);
Toast toast = new Toast(this);

toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setView(layout);
toast.show();

1 个答案:

答案 0 :(得分:2)

吐司膨胀的布局高度仅为100dp。 CENTER变量将toast置于其父布局的中心。使父布局的高度为整个屏幕(match_parent)。然后当你调用setGravity(Gravity.CENTER,0,0)时,它将位于屏幕的中心,即父布局的中心。

吐司:http://developer.android.com/reference/android/widget/Toast.html 重力:http://developer.android.com/reference/android/view/Gravity.html

相关问题