0

如何在 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();
}

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

4

3 回答 3

2

所以 Y 将是 ActionBar 的高度

假设您正在使用 appcompat 库并且您没有更改 ActionBar 高度,这应该可以工作:

int Y = context.getResources().getDimensionPixelSize(R.dimen.abc_action_bar_default_height);
于 2013-08-24T16:11:43.983 回答
1
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
    actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}

它给出了动作栏的高度

于 2013-08-24T16:13:32.327 回答
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>
于 2016-08-30T17:58:47.477 回答