如何获取android中自定义对话框的大小? [英] How to get size of a custom dialog in android?

查看:254
本文介绍了如何获取android中自定义对话框的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从对话框扩展的自定义对话框。
为了放置组件,我需要知道对话框的宽度。我该怎么办?
谢谢



编辑:

  public class AnswerTypeDialog extends Dialog {
public AnswerTypeDialog(Context context){
super(context);
mContext = context;
}

@Override
protected void onCreate(final Bundle savedInstanceState){
super.onCreate(savedInstanceState);
init();
}

protected void init(){
ll =(RelativeLayout)LayoutInflater.from(mContext).inflate(R.layout.answertype_layout,null);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(ll);
mWindowWidth = this.getWindow()。getAttributes()。width;
}
}

mWindowWidth为零

解决方案

这将工作。

  @Override 
public void onWindowFocusChanged(boolean hasFocus){
super.onWindowFocusChanged(hasFocus);
int height = mDialog.getWindow()。getDecorView()。getHeight();
int width = mDialog.getWindow()。getDecorView()。getWidth();
}


I have a custom Dialog which is extended from dialog. in order to place component on it, I need to know the width of the dialog. how could I do it? Thanks

EDIT:

public class AnswerTypeDialog extends Dialog{
    public AnswerTypeDialog(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    protected void init(){
        ll=(RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.answertype_layout, null);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(ll);
        mWindowWidth = this.getWindow().getAttributes().width;
    }
}

mWindowWidth is zero.

解决方案

This will work.

@Override
    public void onWindowFocusChanged (boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        int height=mDialog.getWindow().getDecorView().getHeight();
    int width=mDialog.getWindow().getDecorView().getWidth();
}

这篇关于如何获取android中自定义对话框的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆