1

如何使弹出窗口出现在按钮而不是确切位置下?目前这是我设置弹出窗口出现位置的代码,

popUpFollowUsername.showAtLocation(findViewById(R.id.dashboardRelative), Gravity.TOP, 100, 280);

我希望它直接出现在这个按钮的按钮下方

<Button android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/post"
        android:paddingTop="10dip"
        android:paddingBottom="10dip"
        android:textSize="20sp"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        android:background="@drawable/dashboard_post"
        android:id="@+id/btnPost"/>

我有多部安卓手机,弹出窗口出现在每部手机的不同位置,我怎样才能让它总是直接出现在帖子按钮下?

4

1 回答 1

0

使用View.getLocationOnScreen()and/or View.getLocationInWindow(),您将能够获得屏幕上视图的位置。有了这些信息,应该很容易将弹出窗口放在按钮附近。

基本上使用类似的东西:(未测试)

Button btn = get(); // maybe with findViewById
btn.setOnClickListener(new OnClickListener() {
    void onClick(View v) {
        int[] pos = new int[2];
        v.getLocationInWindow(pos);
        int x = pos[0];
        int y = pos[1] + v.getHeight();
        // show popup at the given x,y position
    }
});
于 2013-07-20T22:27:03.340 回答