我想要让对话框的宽度只有imagebutton的宽度那么大,然后对话框显示的时候在最右边

解决方案 »

  1.   

    楼主,这玩意你得先show()出dialog, 再来设置属性。
    private void show_menu_dlg()
    {
    //加载对话框资源界面
    LayoutInflater factory = LayoutInflater.from(this.getContext());
    final View dlgview = factory.inflate(R.layout.player_menu, null);

    AlertDialog.Builder dlg_login = new AlertDialog.Builder(this.getContext());
    //将对话框布局设置到视图
    dlg_login.setView(dlgview);

    //创建 并显示对话框
    AlertDialog dlg = dlg_login.create();
    dlg.show();
    //设置对话框位置
    LayoutParams lp = dlg.getWindow().getAttributes();
    //dlg.getWindow().setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
    lp.x = m_screen_width - 64;
    lp.width = 64;
    dlg.getWindow().setAttributes(lp);
    dlg.setCanceledOnTouchOutside(true);
    }