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

実際、どのアクティビティからでもこのメソッドを呼び出せるようにするために、Application クラスに入れました。私が理解しているように、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 に答える