Android:活动的位置作为对话框

时间:2013-05-02 22:18:55

标签: android

我希望在我的应用中有一个弹出对话框。该对话框最好作为一项活动完成。然后我在清单中使用“Theme.Dialog”使活动表现为对话框。这很好。

似乎没有效果的是将弹出窗口定位在我想要的位置。

这是弹出窗口的模板,圆形菜单应该以圆形为中心 按钮。只是一个相对布局,在“甜甜圈”周围摆放了六个图标 enter image description here

我想要做的是在屏幕上通过几个圆形按钮之一显示弹出窗口。根据按下的按钮,我收集按下的视图(按钮)的X / Y位置,并通过捆绑包将其发送到活动。然后我想使用按下按钮的位置和弹出窗口的大小来定位对话框,将其置于按钮的中心位置。

public class WirelessDialog extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    //requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.donut_dialogue);

    // get the layout of the dialog
    RelativeLayout rl = (RelativeLayout)findViewById(R.id.donut_layout);

    // get base image all other icons reside on and compute its haLf-width/height
    ImageView iv = (ImageView)findViewById(R.id.donut);
    int half_width = iv.getWidth() / 2 ;
    int half_height = iv.getHeight() / 2;


    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        int startX = extras.getInt("X");
        int startY = extras.getInt("Y");
        int endX = extras.getInt("XE");
        int endY = extras.getInt("YE");

        WindowManager.LayoutParams winParms = getWindow().getAttributes() ;
        int x = winParms.x;
        int y = winParms.y;
        Log.d("DIALOG", "Original x/y: " + x + " " + y);
        //int width = winParms.width;
        //int height = winParms.height;

        int xc = startX + (endX - startX + 1) / 2;
        int yc = startY + (endY - startY + 1) / 2;


        // new location of the dialog should be 1/2 the width and height of the donut subtracted
        // from the center of the button that was pressed.

        winParms.x = (xc - half_width);
        winParms.y = (yc - half_height);
        Log.d("DIALOG", "New    x/y: " + winParms.x + " " + winParms.y);
        getWindow().setAttributes(winParms);
    }

}

设置布局参数仅强制对话框到最右侧。原来,它是 x / y位置返回为零,但任何非零数字甚至不会将其放置在位置指示的位置。

将其放置在0,0是屏幕的中心,并将X / Y位置设置为负值将其强制为左侧的uppper。根据文档

,数学不是我所期望的

任何人都知道我应该做些什么来正确放置这个对话框窗口? 一个典型的例子是1280x800屏幕平板电脑上的(400,470)按钮。

1 个答案:

答案 0 :(得分:0)

在这种情况下我该怎么做:

创建自定义视图类作为循环弹出窗口; 当然,如果选项相同,则在活动初始​​化时实例化此视图; 在按钮上单击侦听器,获取其X,Y,宽度和高度,您的X和Y位置将是,buttonX +(buttonWidth / 2),buttonY +(buttonHeight / 2); 添加此视图并将其放置在此X和Y位置。

为了更快,更“便宜”,您的视图可以在创建活动时实例化,并且在视图布局根元素中是不可见的(android:visible =“none”)。

我认为您的Activity根布局必须是RelativeLayout或FrameLayout。

我认为这个解决方案应该有用。

相关问题