setVisibility(GONE) view becomes invisible but still occupies space

后端 未结 13 2016
我在风中等你
我在风中等你 2020-12-03 03:56

I\'ve got a view that is effectively is a button. Here is its XML layout (add_new.xml)




        
13条回答
  •  生来不讨喜
    2020-12-03 04:40

    All replies in this thread are suggesting a new wrapper view, which comes at a cost. The correct way of hiding a view completely is to set margins to 0 while setting visibility to GONE. In this code sample, cardView is the view I am trying to hide. The direct parent of cardView is RecyclerView, that's why we are using RecyclerView.LayoutParams - remember to replace with the right layout params.

    if (cardView.getVisibility() != GONE) {
        cardView.setVisibility(GONE);
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) cardView.getLayoutParams();
        layoutParams.setMargins(0, 0, 0, 0);
        cardView.setLayoutParams(layoutParams);
    }
    

提交回复
热议问题