Setting PopupWindow to have a maximum height

前端 未结 1 1421
逝去的感伤
逝去的感伤 2021-01-24 02:44

I inflate a ListView inside a PopupWindow and I want the popup to behave like this:

  • wrap the listview when its height is < x
  • set the popup\'s height =
相关标签:
1条回答
  • 2021-01-24 02:55

    I have found the solution by myself. For anyone who will ever stumble upon this problem, the answer is to override the method onMeasure() of the ListView like this:

    public class MyListView extends ListView {
    
    public MyListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public MyListView(Context context) {
        super(context);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
        //set your custom height. AT_MOST means it can be as tall as needed,
        //up to the specified size.
    
        int height = MeasureSpec.makeMeasureSpec(300,MeasureSpec.AT_MOST);
    
        super.onMeasure(widthMeasureSpec,height);               
    }
    }
    
    0 讨论(0)
提交回复
热议问题