在特定控件下显示自定义对话框

时间:2015-11-19 08:04:00

标签: android dialog android-windowmanager

到目前为止,我已尝试使用以下代码在屏幕上的特定位置显示自定义对话框:

    Dialog dlg = new Dialog(MainActivity.this);
    dlg.setContentView(R.layout.customlayout)
    Window window = dlg.getWindow();
    window.setLayout(200, 200);
    WindowManager.LayoutParams wlp = window.getAttributes();
    wlp.x = 20;
    wlp.y = 160;
    wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    window.setAttributes(wlp);
    dlg.show();

现在,问题是由于我使用以下设置,对话框的位置在不同的Android设备中保持不变:

 wlp.x = 20;
 wlp.y = 160;

所以,问题是在特定控件下面设置对话框,在我的情况下是特定的textview。 WindowManager是否有可用的属性?

或任何其他解决方案?

根据评论编辑如下:

public class MainActivity extends AppCompatActivity {
private TextView txt1;
private int location[] = new int[2];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt1 = (TextView) findViewById(R.id.mtxt);



    txt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showPictureialog();
        }
    });


}

private void showPictureialog() {
    Dialog dlg = new Dialog(MainActivity.this);
    dlg.setContentView(R.layout.customlayout);
    Window window = dlg.getWindow();
    window.setLayout(200, 200);
    WindowManager.LayoutParams wlp = window.getAttributes();
    txt1.getLocationOnScreen(location);

    wlp.x = location[0];
    wlp.y = location[1];
    wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    window.setAttributes(wlp);
    dlg.show();
    Toast.makeText(MainActivity.this, "X : " + location[0], Toast.LENGTH_SHORT).show();
    Toast.makeText(MainActivity.this, "Y : " + location[1], Toast.LENGTH_SHORT).show();
}

}

0 个答案:

没有答案