setVisibility(GONE) view becomes invisible but still occupies space

后端 未结 13 1966
我在风中等你
我在风中等你 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:37

    I don't think this is a bug.. Just use GONE to make the empty space disappear. CSS does the same thing. Try display: block vs visibility: hidden.

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-12-03 04:41

    If this is stil needed, there is a great way to deal with it:

    parentView.removeView(childView);
    

    here an example:

    final WhatEverLayout parentView, childView;
    parentView = (WhatEverLayout)findViewById(R.id.parentView_xml);
    childView =(WhatEverLayout)findViewById(R.id.childView_xml);
    parentView.removeView(childView);
    
    0 讨论(0)
  • 2020-12-03 04:44

    What you can do is set an if statement, whenever the condition to set the visibility to "GONE", set the view as wrap content and your space will be free, I did it and it worked for a seekbar

    0 讨论(0)
  • 2020-12-03 04:48

    This is my solution. Create a new layout file with name "special.xml", copy the code to the file.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
        android:visibility="gone">
    
    </LinearLayout>
    

    Use condition to inflate the empty layout inside the newview. Don't Use the view.setVisibility(View.GONE);.

    if(true){
      view=mInflater.inflate ( R.layout.special,parent, false );
    }else{
      view=mInflater.inflate(R.layout.normal,parent,false);
      // The view that you want to be visible
    }
    
    0 讨论(0)
  • 2020-12-03 04:48

    A generic and easy solution is to avoid all thuse heck of code:

    You need to add a Container Layout or a Parent Layout over your set of views that you want to hide. It can be anything like CardView or FrameLayout or etc.

    Just it has to be a parent for that view that you want to hide. And you wont get all those white spaces. Coz it will consider the entire container to hide instead of considering individual views to HIDE thus, avoiding the white spaces.

    0 讨论(0)
提交回复
热议问题