当前位置:编程学习 > wap >>

怎么设置dialog 显示在指定位置上

比如我想把dialog 设置在  屏幕的   200,150,这个点上,怎么设置? --------------------编程问答-------------------- AlertDialog dlg = new AlertDialog.Builder(this).create();

Window w=dlg.getWindow();
WindowManager.LayoutParams lp =w.getAttributes();
//显示位置
lp.x=200;
lp.y=150;
--------------------编程问答-------------------- 系统中所有对话框,默认布局方式都是居中显示,如果想自定义显示位置可以考虑如下方式:

  ……

  Window mWindow = dialog.getWindow();

  WindowManager.LayoutParams lp = mWindow.getAttributes();

  lp.x= xxx;

  lp.y= xxx;

  复制代码

  缺省居中lp.x=0,lp.y=0

  新坐标 x小于0左移,大于0右移;y小于0上移,大于0下移

  @Override

  protected void onDestroy() {

  super.onDestroy();

  android.os.Process.killProcess(android.os.Process.myPid());

  }

  public static String wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords) {

  if (str == null) {

  return null;

  }

  if (newLineStr == null) {

  newLineStr = System.getProperty("line.separator");

  }

  if (wrapLength < 1) {

  wrapLength = 1;

  }

  int inputLineLength = str.length();

  int offset = 0;

  StringBuffer wrappedLine = new StringBuffer(inputLineLength + 32);

  while ((inputLineLength - offset) > wrapLength) {

  if (str.charAt(offset) == ' ') {

  offset++;

  continue;

  }

  int spaceToWrapAt = str.lastIndexOf(' ', wrapLength + offset);

  if (spaceToWrapAt >= offset) {

  // normal case

  wrappedLine.append(str.substring(offset, spaceToWrapAt));

  wrappedLine.append(newLineStr);

  offset = spaceToWrapAt + 1;

  } else {

  // really long word or URL

  if (wrapLongWords) {

  // wrap really long word one line at a time

  wrappedLine.append(str.substring(offset, wrapLength + offset));

  wrappedLine.append(newLineStr);

  offset += wrapLength;

  } else {

  // do not wrap really long word, just extend beyond limit

  spaceToWrapAt = str.indexOf(' ', wrapLength + offset); --------------------编程问答--------------------  if (spaceToWrapAt >= 0) {

  wrappedLine.append(str.substring(offset, spaceToWrapAt));

  wrappedLine.append(newLineStr);

  offset = spaceToWrapAt + 1;

  } else {

  wrappedLine.append(str.substring(offset));

  offset = inputLineLength;

  }

  }

  }

  }

  // Whatever is left in line is short enough to just pass through

  wrappedLine.append(str.substring(offset));

  return wrappedLine.toString();

  } --------------------编程问答-------------------- protected void setDialogAttributes(Dialog dlg, boolean right, int xpos, int ypos){
try{
        Window win = dlg.getWindow();
        WindowManager.LayoutParams wl = win.getAttributes();
        if(right){
         wl.gravity = android.view.Gravity.RIGHT | android.view.Gravity.TOP;
        }else{
         wl.gravity = android.view.Gravity.LEFT | android.view.Gravity.TOP;
        }
        wl.x      = xpos;
        wl.y      = ypos;
        wl.height = 500;
        wl.flags  = WindowManager.LayoutParams.FLAG_DIM_BEHIND 
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS ;
        wl.alpha = 1.0f;
        wl.dimAmount = 0.0f;
        win.setAttributes(wl);
}
catch(Exception e){
Log.e(TAG, "setWinAttributes", e);
}
} --------------------编程问答-------------------- 楼主真的应该结贴的,这样别人还知道谁的答案是对的可以参考一下,若都不对,楼主把你的贴上来说说解决办法。。。哎。。。。
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,