如何在ActionBar下面显示自定义吐司

时间:2013-08-24 16:01:47

标签: android toast android-actionbar-compat

如何在ActionBar下方显示自定义Toast(与API7兼容)?

创建Toast的方法如下:

public void showToast(Activity context, String text) {
    LayoutInflater inflater = context.getLayoutInflater();
    View layout = inflater.inflate(R.layout.popup, null);
    TextView tv = (TextView)layout.findViewById(R.id.popup);
    tv.setText(text);
    Toast toast = new Toast(context);
    toast.setGravity(Gravity.FILL_HORIZONTAL, 0, Y);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();
}

实际上,为了能够从任何活动中调用此方法,我将它放在我的Application类中。据我所知,我需要将Y(顶部偏移)放到toast.SetGravity()中。但我不知道如何获得活动布局的正确顶部坐标,即“ActionBar bottom”。任何帮助将非常感谢!

3 个答案:

答案 0 :(得分:2)

所以Y将是ActionBar的高度

假设你正在使用appcompat库并且你没有改变ActionBar高度,这应该可行:

int Y = context.getResources().getDimensionPixelSize(R.dimen.abc_action_bar_default_height);

答案 1 :(得分:1)

TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
    actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}

它提供了操作栏高度

答案 2 :(得分:0)

使用我写过的课程:

public class TopToast
{
    private TextView m_tv = null;
    private Toast m_Toast = null;
    private Activity m_Activity = null;

    public TopToast(Activity activity)
    {
        m_Activity = activity;
        m_Toast = new Toast(m_Activity);
    }

    public void show(String text, int TextColor)
    {
        LayoutInflater inflater = m_Activity.getLayoutInflater();
        View layout = inflater.inflate(R.layout.popup, null);
        m_tv = (TextView)layout.findViewById(R.id.popup);
        m_tv.setTextColor(ContextCompat.getColor(m_Activity, TextColor));
        m_tv.setText(text);

        final TypedArray styledAttributes = m_Activity.getTheme().obtainStyledAttributes(new int[] { android.R.attr.actionBarSize });
        int Y = (int) styledAttributes.getDimension(0, 0);
        styledAttributes.recycle();

        m_Toast.setGravity(Gravity.TOP | Gravity.START | Gravity.FILL_HORIZONTAL, 0, Y);
        m_Toast.setDuration(Toast.LENGTH_LONG);
        m_Toast.setView(layout);
        m_Toast.show();
    }

    // Call this if you wish to hide toast quickly (viz. from onPause of activity, so that if user closes activity quickly toast too will disappear)
    public void hide()
    {
        if (m_Toast!=null)
            m_Toast.cancel();

       if (m_tv!=null)
            m_tv.setVisibility(View.GONE);
    }
}

和xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content">
    <TextView
        android:id="@+id/popup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textColor="@color/red"
        android:background="@color/white"
        android:textAppearance="?android:attr/textAppearanceSmallPopupMenu"/>
</LinearLayout>