setVisibility(GONE) view becomes invisible but still occupies space

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

    You can use : viewToHide?.postDelayed({ viewToHide?.visibility = View.GONE }, durationInMillis)

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

    For me, it worked if I did the showing/hiding on the next run loop.

    parentView.post { 
       childViewToHide.visibility = View.GONE
    }
    
    0 讨论(0)
  • 2020-12-03 04:52

    I had similar issue when using TransitionManager.beginDelayedTransition(), in the layout inspector I see that the view is Gone but still taking space, I think it is bug in Android, a workaround is to execute the visibility block with View.post(Runnable action)

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

    set layout_width and layout_height to 0 where you want to hide item, by doing this

    //if item's parent layout is LinearLayout, change according to your item xml
    holder.itemView.setLayoutParams(new LinearLayout.LayoutParams(0,0));
    
    0 讨论(0)
  • 2020-12-03 04:55
    If you want to show itemView 
    
    if (ItemBean.isShow()) 
    {
       holder.itemView.setVisibility(View.VISIBLE);
       holder.itemView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
     } else
     {
         holder.itemView.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
         holder.itemView.setVisibility(View.GONE);
     }
    
    0 讨论(0)
  • 2020-12-03 04:57

    header_layout.xml:

    <?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">
            <LinearLayout
                android:id="@+id/mHeaderView"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/mNoHelpLayout"
                    android:layout_width="match_parent"
                    android:layout_height="176dip"
                    android:background="#ffffffff"
                    android:gravity="center"
                 />
           </LinearLayout>
       </LinearLayout>
    

    java code:

    LayoutInflater mInflater =(LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View outerView =  mInflater.inflate(R.layout.header_layout, null);
    mHeaderView = (LinearLayout) outerView.findViewById(R.id.mHeaderView);
    

    use mHeaderView.setVisiable() to control visiable not the outerView.setVisiable(). it works for me.

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