ConstraintLayout, when constraint dependent view is gone, the layout view behave weirdly

后端 未结 4 503
执念已碎
执念已碎 2020-12-16 09:18

I\'m using ConstraintLayout where I will show as below

I would like to hide First (using gone), and which the view I expect to be as below (whe

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 09:47

    Say, you want to have a picture like this:

    Here you have indents between the title and "Nice work", between "Nice work" and time, also horizontal indent to "Opinions". They are centered vertically.

    "Opinions" is attached to the star, so that can be multilined and stay centered. I show results for 2 variants: in the first row opinions are multilined, while in the next row it is a single line. In columns you can see 4 variants of showing/hiding 2 labels.

    1. A more simple and preferrable way is to wrap both labels into LinearLayout and insert it into parent ConstraintLayout. Then you can set vertical gravity, show or hide labels, hide the LinearLayout itself.

    2. If you don't want to have nested layouts, use Barriers and Groups. It is a difficult task that can waste many hours. A key is having additional Views for aligning. Here I have 2 hiding labels ("Nice work" and "Opinions"), and I have to add 2 views (spaces).

    The height of the right space is equal to the height of the star (14dp).

    To simplify hiding several views, I joined them into groups.

    You can see horizontal dotted lines - they are Barriers. I align them on tops and bottoms of the most big views (barrier_2 is similar):

    
    

    Vertical aligning is based on these 2 additional Spaces (see marginTop="10dp"):

    
    

    It is difficult to cover all situations, so see the following layout:

    
    
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
    
    

    Then in your activity you can show/hide labels. Hide Groups, not views inside, because strangely inside a Group views are always visible.

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
    
        left_text_group.visibility = View.GONE
        opinion_group.visibility = View.VISIBLE
    }
    

提交回复
热议问题