开发者

scrollView in AlertDialog body not scrolling

开发者 https://www.devze.com 2023-03-03 22:26 出处:网络
I am trying to have a ScrollView in the message part of the AlertDialog. But, i am unable to get the desired result. Following is the relevant piece of code. If you find something incorrect in the way

I am trying to have a ScrollView in the message part of the AlertDialog. But, i am unable to get the desired result. Following is the relevant piece of code. If you find something incorrect in the way i am building the custom view, please let me know. TIA.

protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_SEARCH:
        dialogLayoutOuter = new ScrollView(this);
        LayoutParams scroll_lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        dialogLayoutOuter.setLayoutParams(scroll_lp);
        dialogLayoutOuter.setFillViewport(true);
        dialogLayoutOuter.setVerticalScrollBarEnabled(true);

        dialogLayout = new LinearLayout(this);
        LinearLayout.LayoutParams lp_lv = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        dialogLayout.setLayoutParams(lp_lv);            
        dialogLayout.setOrientation(LinearLayout.VERTICAL)开发者_JAVA百科;

        int bgColor = 0xFF00FFFF;
        int bgColorBlack = 0xFF000000;
        LinearLayout.LayoutParams lp_divider = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,2);
        LinearLayout.LayoutParams lp_text = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        LinearLayout.LayoutParams lp_lv1 = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        ListIterator<ListView> itr = myListViewList.listIterator();
        while(itr.hasNext()) {
            ListView lv = itr.next();
            TextView myTextView = new TextView(this);
            myTextView.setText(lv.getTag().toString());
            myTextView.setBackgroundColor(bgColor);
            myTextView.setTextColor(bgColorBlack);
            myTextView.setGravity(Gravity.CENTER);
            dialogLayout.addView(myTextView, lp_text);
            LinearLayout llDivider = new LinearLayout(this);
            llDivider.setBackgroundColor(bgColorBlack);
            dialogLayout.addView(llDivider, lp_divider);
            dialogLayout.addView(lv, lp_lv1);
        }

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

        dialogLayoutOuter.addView(dialogLayout);
        dialogBuilder.setView(dialogLayoutOuter);           
        activityDialog = dialogBuilder.create();
        return activityDialog;
    }
    return null;
}


A ListView doesn't work inside a ScrollView, because both have a scrolling mechanism built in.

I recommend to replace the ListViews you create with simple LinearLayouts. Easiest way is to create an xml layout, that contains everything you would show inside a row and an xml layout with the ScrollView.

Inflate the ScrollView with LayoutInflater, find the LinearLayout where you want to add your rows and add them there. Finally set the ScrollView as a View of your Dialog.

0

精彩评论

暂无评论...
验证码 换一张
取 消