Issue with RelativeLayout when View visibility is View.GONE

后端 未结 8 863
自闭症患者
自闭症患者 2021-02-02 05:11

I\'ve a RelativeLayout thus:



 // <-- View.VISIBLE OR View.GONE

<         


        
相关标签:
8条回答
  • 2021-02-02 05:19

    you can do this

    <RelativeLayout>
    <TextView1/>
    <FrameLayout>
      <TextView2/>  // <-- View.VISIBLE OR View.GONE
    </FrameLayout>
    <TextView3/>
    <TextView4/>
    </RelativeLayout>
    

    let TextView3 below this FrameLayout which has no background, so if TextView2 is Gone ,it doesn't occupy space.

    0 讨论(0)
  • 2021-02-02 05:20

    Forget about INVISIBLE or GONE, use this instead:

    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
    
    params.height = 0;
    
    params.setMargins(0,0,0,0);
    
    view.setLayoutParams(params);
    
    0 讨论(0)
  • 2021-02-02 05:28

    This answer does not solve your specific problem, but does solve a similar one, so hopefully this will help somebody.

    I had a situation where my relative layout did not have the equivalent of your TextView1. So, in my situation, if TextView2 was GONE, then I wanted TextView3 to be aligned with the parent's top. I solved that by adding to TextView3 the attribute android:layout_alignWithParentIfMissing="true". See http://developer.android.com/resources/articles/layout-tricks-efficiency.html.

    Unfortunately, I do not see a way to specify an alternate alignment anchor unless it is the parent.

    0 讨论(0)
  • 2021-02-02 05:29

    place all textViews under LinearLayout with vertical orientation.

    <LinearLayout>
    <TextView/>
    <TextView/>
    <TextView/>
    <TextView/>
    <TextView/>
    <TextView/>
    </LinearLayout>
    
    0 讨论(0)
  • 2021-02-02 05:31

    You can place textview 2 and 3 in the LinearLayout and keep the linear layout below textview 1.

    0 讨论(0)
  • 2021-02-02 05:35

    why not update the below attribute of TextView3 when you update the visibility of TextView2? (I assume you do this in code)

    something like

    TextView tv = (TextView) findViewById(R.id.textview3);
    RelativeLayout.LayoutParams lp =
        (RelativeLayout.LayoutParams) tv.getLayoutParams();
    lp.addRule(RelativeLayout.BELOW, R.id.textview1);
    ((TextView) view).setLayoutParams(lp);
    
    0 讨论(0)
提交回复
热议问题