Not getting RelativeLayout's getHeight() after setting Visibility Visible from Visiblity GONE

后端 未结 4 892
醉话见心
醉话见心 2021-01-15 04:57

What i want is, when i click on Dashboard Button it will open like a SlidingDrawer and after it opened when clicked on it again it will close. i use this custom drawer becau

4条回答
  •  离开以前
    2021-01-15 05:38

    When a View is first created, it's width and height are not available to you until onMeasure() has been called. Due to this, the first time you try to get the values, they have not been assigned yet.

    Since you're using a custom View, the solution is pretty simple. Simply move the code that gets the height and width into onMeasure() and use it after that.

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
        width = getWidth(); // Get View Width
        height = getHeight();// Get View Height
    } 
    

提交回复
热议问题