警报对话框列表视图长度

时间:2018-02-20 17:22:11

标签: java android listview android-dialog

我想将AlertDialog设置为ListView内部的长度,AlertDialog底部有一个按钮,即使列表包含,当前也会跨越整个屏幕1或2项,我怎么能实现这一点,

到目前为止,这是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lvItemsSelected"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/buttonOk"/>
    <Button
        android:id="@+id/buttonOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:text="OK"/>
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

如果这可以帮助您,请在将数据填充到ListView

后尝试调用此方法
public void getListViewSize(ListView myListView, Context context) {
    ListAdapter myListAdapter = myListView.getAdapter();

    if (myListAdapter == null) {
        return;
    }

    int totalHeight = 0;
    for (int size = 0; size < myListAdapter.getCount(); size++) {

        View listItem = myListAdapter.getView(size, null, myListView);
        if (listItem instanceof ViewGroup)
            listItem.setLayoutParams(new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT));

        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        @SuppressWarnings("deprecation")
        int screenWidth = display.getWidth();
        int listViewWidth = screenWidth - 65;
        int widthSpec = View.MeasureSpec.makeMeasureSpec(listViewWidth,
                View.MeasureSpec.AT_MOST);
        listItem.measure(widthSpec, 0);

        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = totalHeight
            + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
    myListView.setLayoutParams(params);
    myListView.requestLayout();
}

答案 1 :(得分:0)

您可以使用RelativeLayout代替AlertDialog,然后将ListView的约束与logOutFile_fd = fopen(logOutFile,"w"); if (logOutFile_fd==NULL) { /* omitted */ }

的宽度相关联