如何在Android上获得AlertDialog的高度

时间:2013-05-02 06:28:10

标签: android alertdialog android-alertdialog

我使用下面的代码显示一个对话框:

private AlertDialog MyDialog;
public void adddialog() {
    AlertDialog.Builder MyBuilder = new AlertDialog.Builder(this);
    MyBuilder.setTitle("Title")
    MyBuilder .setView(ListView);
    MyDialog = MyBuilder.create();
}

ListView可能会取消修复行 但是我希望得到MyDialog的高度 我该怎么做才能搞定?

3 个答案:

答案 0 :(得分:0)

您可以使用listView对象的getHeight()而不是获取对话框的高度。

答案 1 :(得分:0)

为了获得ListView可见高度,您需要获得列表的one row高度并乘以元素数量(目前可见)。

像这样。

        Adapter ListAdapter = listview.getAdapter();
        View mView  = ListAdapter.getView(0, null, listview);
        mView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int requiredHeight = listview.getChildCount() * mView.getMeasuredHeight();

答案 2 :(得分:0)

我检查了高度,这意味着有关视图有多高的信息.. 也许你可以尝试这样: 你可以通过以下方式获得高度:

     int height = MyDialog.getWindow().getDecorView().getHeight();

此外,您可以设置高度:

     MyDialog.getWindow().setLayout(width, height);

注意:应在MyDialog.show();

之后调用所有方法
相关问题